将时间戳转换为日期,并在pandas列中的列表中只保留日期



我有一个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

最新更新