python - 值错误 : can only call with other PeriodIndex-ed objects

标签 python pandas join dataframe

我正在尝试将 2 个数据帧合并在一起。具有讽刺意味的是,它们最初是同一个数据框的一部分,但我正在迈出一小步——有时是在错误的方向上。 第 1 帧看起来像这样:


Int64Index: 10730 entries, 0 to 10729
Data columns (total 6 columns):
RegionID      10730 non-null int64
RegionName    10730 non-null object
State         10730 non-null object
Metro         10259 non-null object
CountyName    10730 non-null object
SizeRank      10730 non-null int64
dtypes: int64(2), object(4)

Frame 2 looks like this:


Int64Index: 10730 entries, 0 to 10729
Data columns (total 82 columns):
1996Q2    8218 non-null float64
1996Q3    8229 non-null float64
1996Q4    8235 non-null float64
.....
2016Q1    10730 non-null float64
2016Q2    10730 non-null float64
2016Q3    10730 non-null float64
dtypes: float64(82)

Notice that the indexes are of the same type, and they even have the same number of rows.
I am trying to merge the dataframes back together like so:

df4 = pd.merge(df3, df2, how='inner', left_index=True, right_index=True)

我得到的错误是:

ValueError: can only call with other PeriodIndex-ed objects

第二个数据框中的 2016Q1 和类似名称的列是 Period 类型,但我没有合并它们——我认为只要索引排成一行,合并就应该起作用?我做错了什么?

最佳答案

假设我们有以下 DF:

In [44]: df1
Out[44]:
   1996Q2  2000Q3    2010Q4
0     1.5     3.5  1.000000
1    22.0    38.5  2.000000
2    15.0    35.0  4.333333

In [45]: df1.columns
Out[45]: PeriodIndex(['1996Q2', '2000Q3', '2010Q4'], dtype='period[Q-DEC]', freq='Q-DEC')

注意:df1.columns 属于PeriodIndex 数据类型

In [46]: df2
Out[46]:
    a   b   c
0  a1  b1  c1
1  a2  b2  c2
2  a3  b3  c3

In [47]: df2.columns
Out[47]: Index(['a', 'b', 'c'], dtype='object')

mergejoin 将返回:ValueError: can only call with other PeriodIndex-ed objects 因为,据我所知,Pandas DF 不能如果其中一些属于 PeriodIndex dtype,则具有混合列数据类型:

In [48]: df1.join(df2)
...
skipped
...
ValueError: can only call with other PeriodIndex-ed objects

merge 抛出相同的异常:

In [54]: pd.merge(df1, df2, left_index=True, right_index=True)
...
skipped
...
ValueError: can only call with other PeriodIndex-ed objects

因此我们必须将 df1.columns 转换为字符串:

In [49]: df1.columns = df1.columns.values.astype(str)

In [50]: df1.columns
Out[50]: Index(['1996Q2', '2000Q3', '2010Q4'], dtype='object')

现在 joinmerge 将起作用:

In [51]: df1.join(df2)
Out[51]:
   1996Q2  2000Q3    2010Q4   a   b   c
0     1.5     3.5  1.000000  a1  b1  c1
1    22.0    38.5  2.000000  a2  b2  c2
2    15.0    35.0  4.333333  a3  b3  c3

In [52]: pd.merge(df1, df2, left_index=True, right_index=True)
Out[52]:
   1996Q2  2000Q3    2010Q4   a   b   c
0     1.5     3.5  1.000000  a1  b1  c1
1    22.0    38.5  2.000000  a2  b2  c2
2    15.0    35.0  4.333333  a3  b3  c3

合并 DF 的列 dtypes:

In [58]: df1.join(df2).columns
Out[58]: Index(['1996Q2', '2000Q3', '2010Q4', 'a', 'b', 'c'], dtype='object')

如果在合并完成后需要 df1.columns 作为 PeriodIndex - 您可以在转换和设置之前保存 df1.columns完成合并/加入后他们回来:

In [60]: df1.columns
Out[60]: PeriodIndex(['1996Q2', '2000Q3', '2010Q4'], dtype='period[Q-DEC]', freq='Q-DEC')

In [61]: cols_saved = df1.columns

In [62]: df1.columns = df1.columns.values.astype(str)

In [63]: df1.columns
Out[63]: Index(['1996Q2', '2000Q3', '2010Q4'], dtype='object')

# merging (joining) or doing smth else here ...

In [64]: df1.columns = cols_saved

In [65]: df1.columns
Out[65]: PeriodIndex(['1996Q2', '2000Q3', '2010Q4'], dtype='period[Q-DEC]', freq='Q-DEC')

关于python - 值错误 : can only call with other PeriodIndex-ed objects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40499756/

相关文章:

MySQL 从由第 3 个连接的 2 个表中选择(mysql 连接)

python - 如何禁用 Requests 库中的日志消息?

python - 如何将 QWidget() 放在 QStatusBar() 的右端

python - Formalchemy Fieldset 内字段的自定义排序

python - 如何根据 pandas groupby 中的另一个系列获得最大值

sql - 将连接表的列旋转为行

Python分析图像中的特定区域

python - 带有 bins 参数的 pandas value_counts

python - 使用变量作为文件名将 pandas df 写入文件

MySQL JOIN 4个不同的表