将 pandas 系列时间戳转换为唯一日期列表 [英] Converting pandas series timestamp to list of unique dates

查看:134
本文介绍了将 pandas 系列时间戳转换为唯一日期列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在pandas数据框中有一个时间戳格式的列,想将唯一的日期(没有时间)提取到列表中.我尝试过以下方法并没有真正起作用,

I have a column in pandas dataframe in timestamp format and want to extract unique dates (no time) into a list. I tried following ways doesn't really work,

1. dates =  datetime.datetime(df['EventTime'].tolist()).date()
2. dates =  pd.to_datetime(df['EventTime']).date().tolist()
3. dates =  pd.to_datetime(df['EventTime']).tolist().date()

有人可以帮忙吗?

推荐答案

您可以使用dt访问系列中的日期时间对象,请尝试以下操作:

You can use dt to access the date time object in a Series, try this:

pd.to_datetime(df['EventTime']).dt.date.unique().tolist()
# [datetime.date(2014, 1, 1), datetime.date(2014, 1, 2)]


df = pd.DataFrame({"EventTime": ["2014-01-01", "2014-01-01", "2014-01-02 10:12:00", "2014-01-02 09:12:00"]})

这篇关于将 pandas 系列时间戳转换为唯一日期列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆