2016-11-09 72 views
3

我试图合并2个数据帧在一起。具有讽刺意味的是,它们是作为同一数据框架的一部分而开始的,但我正在做出宝贝级的步骤 - 有时候是在错误的方向上。 1帧看起来是这样的:ValueError:只能与其他PeriodIndex对象调用

 

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) 

注意的指标是同一类型的,他们甚至有相同的行数。
我试图合并dataframes重新走到一起,像这样:

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

我得到的错误是:

ValueError: can only call with other PeriodIndex-ed objects 

在第二个数据帧的2016Q1及类似名称的列是周期型的,但我并没有合并他们 - 我认为只要指数排列,合并应该工作?我究竟做错了什么?

+1

你能后降低的样品([mvce(http://stackoverflow.com/help/mcve ))你的两个帧的数据?我认为你的代码绝对没有问题(也就是说,如果df3和df2确实是第1帧和第2帧) –

回答

0

假设我们有以下的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 D型的

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的,据我所知,熊猫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 

dtypes的合并DF:

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

如果您需要df1.columnsPeriodIndex合并完成后 - 你可以节省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') 
+0

我明白你在做什么以及你要去哪里,但是,对于我来说,字符串转换工作并不一样办法。当我这样做“astype”你已经建议,我得到索引(['105','106','107'......],dftype ='object') – alernerdev

+0

@alernerdev,你的熊猫是什么版?我使用熊猫0.19.1 – MaxU

+0

其0.18.1 - 你认为它有所作为? – alernerdev

0

其实我有同样的问题,并获得整数列以及。

而不是

df1.columns = df1.columns.values.astype(str) 

我用

df1.columns = df1.columns.format() 

希望这有助于