我有一个df,看起来像:
df
date
[2021-08-31 00:00:00]
[2021-07-02 00:00:00, 2021-07-02 00:00:00]
[2021-08-31 00:00:00, 2021-09-15 00:00:00]
当我把它导出到.csv
时,我得到一个列,看起来像:
date
[Timestamp('2021-08-31 00:00:00')]
[Timestamp('2021-07-02 00:00:00'), Timestamp('2021-07-02 00:00:00')]
[Timestamp('2021-08-31 00:00:00'), Timestamp('2021-09-15 00:00:00')]
我希望.csv
文件在没有Timestamp
的情况下与df
中的外观相同,最好没有小时、分钟和秒。
我尝试过的:
检查我是否可以这样转换:
for nr, item in enumerate(df['date']):
print(pd.to_datetime(item[nr],format='%Y%m%d'))
print(type(item[nr]))
break
但它仍然返回秒,我不明白为什么我添加了format
参数。
2021-08-31 00:00:00
<class 'pandas._libs.tslibs.timestamps.Timestamp'>
我试过了:
def to_date(df):
res = []
for nr, item in enumerate(df['date']):
res.append(item[nr].date())
return res
df['test'] = to_date(df)
但我得到了:
IndexError:列出超出范围的索引
我不确定应该如何转换它,以便在我的.csv
文件中获得类似于此2021-08-31
的日期。
尝试通过map()
和列表理解:
df['date']=df['date'].map(lambda x:[str(y) for y in x])
#you can also use apply() or agg() but they are slower then map()
最后使用to_csv()
方法:
df.to_csv('filename.csv')
更新:
如果你只想要日期部分,那么:
df['date']=df['date'].map(lambda x:[str(y.date()) for y in x])
#as suggested by @MrFuppes