ValueError:只能与其他PeriodIndex-ed对象一起调用 [英] ValueError: can only call with other PeriodIndex-ed objects

查看:62
本文介绍了ValueError:只能与其他PeriodIndex-ed对象一起调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to merge 2 dataframes together. Ironically, they started out as part of the same dataframe, but I am making baby steps -- sometimes in the wrong direction. Frame 1 looks like this:



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)

第2帧看起来像这样:



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类型,但是我没有合并它们-我认为只要索引对齐,合并就可以了吗?我在做什么错了?

The 2016Q1 and similarly named columns in the 2nd dataframe are of Period type, but I am not merging on them -- I thought as long as the indexes line up, merge should work? What am i doing wrong?

推荐答案

假设我们有以下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.columnsPeriodIndex dtype

Notice: df1.columns are of the PeriodIndex dtype

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,因为AFAIK,Pandas DF不能具有混合列dtype,如果其中某些类型是PeriodIndex dtype:

merge and join will return: ValueError: can only call with other PeriodIndex-ed objects as, AFAIK, Pandas DF can't have a mixed column dtypes if some of them are of 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转换为字符串:

So we will have to convert df1.columns to strings:

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,并在完成合并/合并后将其重新设置:

If you need df1.columns as PeriodIndex after the merging is done - you can save df1.columns before you convert them and set them back after you are done with merging / joining:

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')

这篇关于ValueError:只能与其他PeriodIndex-ed对象一起调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆