'时间戳'对象不可迭代

时间:2017-04-30 16:20:12

标签: python python-2.7 pandas dataframe timestamp

我试图将pandas.core.series.Series(df1 [' col1'])与pandas.core.frame.DataFrame(df2)相乘。他们的指数是相同的,但它总是让我回归' '时间戳'对象不可迭代'当我试图通过做

来增加它们
k = df1['col1'].mul(df2)


[In] df1:
[out]
Index         col1    col2
2065-12-20     12      apples
2061-10-31     12      candies
2045-11-28     70      marshmalow
2043-10-31     11      apples
2040-07-30     21      cars
2049-06-30     64      planes
2036-01-31     14      glasses



[In] df2: 
Index             col1    col2   etc.... 
2065-12-20         14      120
2061-10-31         18      800
2045-11-28         19      580
2043-10-31         21      12
2040-07-30         44      21
2049-06-30         1.2     17
2036-01-31         61.8    61 

我想要

Index             col1        col2   etc.... 
2065-12-20         14*12      120*12
2061-10-31         18*12      800*12
2045-11-28         19*70      580*70
2043-10-31         21*11      12*11
2040-07-30         44*21      21*21
2049-06-30         1.2*64     17*64
2036-01-31         61.8*14    61*61

df1 [' col1']是在几天前,我使用

转换它
df1['col1'] = (df1['col1'].values / np.timedelta64(1, 'D')).astype(int)

df1.dtypes = dtype(' int32')

任何想法为什么会抛出错误?

1 个答案:

答案 0 :(得分:1)

出现异常是因为您使用整个DataFrame(包括时间戳列!)而不是单个列:

df1['col1'].mul(df2['col1'])

根据您可能想要的数据框架:

df1['col1'].values[:, None] * df2[['col1', 'col2', ...]]  # explicitly list columns

df1['col1'].values[:, None] * df2[df2.columns[1:]]        # all columns except the first

如果您想一次处理多个列:

first group of lines.
first group of lines.
first group of lines.
first group of lines.
first group of lines.
first group of lines.

second group of lines. 
second group of lines. 
second group of lines. 
second group of lines. 
second group of lines. 
second group of lines.