- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我可以像这样创建季度和月度 PeriodIndex:
idx = pd.PeriodIndex(year=[2000, 2001], quarter=[1,2], freq="Q") # quarterly
idx = pd.PeriodIndex(year=[2000, 2001], month=[1,2], freq="M") # monthly
我希望能够像这样创建一个年度 PeriodIndex:
idx = pd.PeriodIndex(year=[2000, 2001], freq="Y")
相反,这会引发以下错误:
Traceback (most recent call last):
File ".../script.py", line 3, in <module>
idx = pd.PeriodIndex(year=[2000, 2001], freq="Y")
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/indexes/period.py", line 250, in __new__
data, freq2 = PeriodArray._generate_range(None, None, None, freq, fields)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/arrays/period.py", line 316, in _generate_range
subarr, freq = _range_from_fields(freq=freq, **fields)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/core/arrays/period.py", line 1160, in _range_from_fields
ordinals.append(libperiod.period_ordinal(y, mth, d, h, mn, s, 0, 0, base))
File "pandas/_libs/tslibs/period.pyx", line 1109, in pandas._libs.tslibs.period.period_ordinal
TypeError: an integer is required
这看起来应该很容易做,但我不明白哪里出了问题。有人可以帮忙吗?
最佳答案
由于当前的实现(至少通过 pandas 1.5.1),
month
和 year
都是必需的“字段”。大多数其他字段值将配置为默认值,但是,如果未提供值,则不会定义 month
或 year
。因此,在这种情况下,月份将保持 None
,这会导致显示的错误
TypeError: an integer is required
Here is a link to the relevant section of the source code其中定义了默认值。省略月份字段会导致 [None, None]
(在本例中)无法转换为 Periodindex。
正确的索引可以按如下方式建立。
idx = pd.PeriodIndex(year=[2000, 2001], month=[1, 1], freq='Y')
导致:
PeriodIndex(['2000', '2001'], dtype='period[A-DEC]')
根据年数,以编程方式生成月份列表也可能有意义:
years = [2000, 2001]
idx = pd.PeriodIndex(year=years, month=[1] * len(years), freq='Y')
作为替代方案,使用 to_datetime 可能更容易+ to_period从 Datetime 索引创建 Period 索引(因为它已经是兼容的形式)
pd.to_datetime([2000, 2001], format='%Y').to_period('Y')
产生相同的 PeriodIndex:
PeriodIndex(['2000', '2001'], dtype='period[A-DEC]')
关于python - 如何设置每年一次的 Pandas PeriodIndex?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74215857/
我可以像这样创建季度和月度 PeriodIndex: idx = pd.PeriodIndex(year=[2000, 2001], quarter=[1,2], freq="Q") # quarte
我的数据框的列为季度(PeriodIndex) - 例如。 2010 年第一季度、2010 年第二季度。此外,还有多个同名列,因为我已将每月数据转换为季度数据。我想要从 2011 年第 1 季度开始的
我正在尝试使用重新采样周期的初始值,以便与 first() 和 last() 一起使用。例如,我从每日数据开始,然后重新采样为每月数据: df.resample('M', kind='period')
我有一个 DataFrame,其中列是按月的 PeriodIndex,如下所示: df = pd.DataFrame(np.random.randn(3,4), index = np.arange(3
Pandas Series.to_json()函数在使用 PeriodIndex 时创建不可读的 JSON。 发生的错误是:json.decoder.JSONDecodeError: Expectin
我正在尝试获取多索引数据框中的最后一个可行日期: ix = pd.MultiIndex.from_product([["a"], pd.PeriodIndex(start="2017-01", end
我希望使用 periodIndex 系列并创建一个新系列,以“yyyy/yy”格式显示当前财政年度。例如,以英国财政年度为例 -> 01/04 至 31/03。 df = pd.DataFrame
pandas.PeriodIndex 上的文档举如下构造示例: >>> idx = PeriodIndex(year=year_arr, quarter=q_arr) 并指定这两个参数的类型: yea
我有一个关于将 tseries.period.PeriodIndex 转换为日期时间的问题。 我有一个如下所示的 DataFrame: colors count
我在绘制具有 PeriodIndex 的 pandas 数据框时遇到问题。 我的数据存在差距,我希望实现以下目标: 间隙应保持间隙,不进行插值; 由于每个值在整个时间段内都有效,因此该值应在整个时间段
在 DataFrame 上使用 panda 的 resample 函数以将刻度数据转换为 OHLCV 时,遇到重采样错误。 我们应该如何解决这个错误? data = pd.read_csv('tick
我正在处理一些组织为带有 MultiIndex 的 df 的财务数据。包含股票代码和日期以及包含返回的列。我想知道是否应该将索引转换为 PeriodIndex而不是 DateTimeIndex因为返回
我正在尝试将 2 个数据帧合并在一起。具有讽刺意味的是,它们最初是同一个数据框的一部分,但我正在迈出一小步——有时是在错误的方向上。第 1 帧看起来像这样: Int64Index: 10730 ent
我在 Python 中有一个 pandas 数据框,其中有一列如下: df.Timestamp ... .................. 129 2018-09-12 21:40:00
我正在尝试对常用的航空公司乘客数据集运行基本的season_decompose,该数据集以以下行开头: Month 1949-02 4.770685 1949-03 4.882802 19
我有一些要分析的订单数据。 目前感兴趣的是:哪个SKU在哪个月被购买的频率? 这里有一个小例子: import datetime import pandas as pd import numpy as
我在 pandas 中有这个数据框 key date story_point Story point 0 SOF-15
file_location3 = "F:/python/course1_downloads/City_Zhvi_AllHomes.csv" housing = pd.read_csv(file_loc
请帮帮我。我想基于 1D 重新采样。我有以下格式的数据。我想在 Pandas 中使用重采样。 我想根据日期和产品重新采样并填充缺失值。 但我一直犯这个错误:我尝试了 5 个选项,错误只在“instan
我是一名优秀的程序员,十分优秀!