我有以下数据框:
df = pd.DataFrame({'user': ['Andrea', 'Gioele'],
'year': [1983, 2014],
'month': [11, 1],
'day': [8, 11]} )
然后我以两种方式为每一行创建日期。第一的:
df['dateA'] = df.apply(lambda x: datetime.date(x['year'],x['month'],x['day']), axis=1)
第二:
df['dateB'] = pd.to_datetime(df[['year','month','day']])
我有以下数据框:
>>> df
10: day month user year dateA dateB
0 8 11 Andrea 1983 1983-11-08 1983-11-08
1 11 1 Gioele 2014 2014-01-11 2014-01-11
我有两种不同的格式:
>>> df['dateA']
1983-11-08
2014-01-11
Name: dateA, dtype: object
>>> df['dateB']
1983-11-08
2014-01-11
Name: dateB, dtype: datetime64[ns]
而且:
>>> df['dateA'].iloc[0]
datetime.date(1983, 11, 8)
>>> df['dateB'].iloc[0]
Timestamp('1983-11-08 00:00:00')
问题是使用第一种方法计算日期非常昂贵,所以我想转换df['dateB']
它以使其具有“对象”格式。有办法吗?
注意:我已经尝试过可能的“重复问题”建议(它们总是有字符串,而不是时间戳),但我得到以下
>>> datetime.datetime.fromtimestamp(df['dateB'].iloc[0])
Traceback (most recent call last):
File "<pyshell#68>", line 1, in <module>
datetime.datetime.fromtimestamp(df['dateB'].iloc[0])
TypeError: a float is required