pandas 将熊猫系列时间戳转换为唯一日期列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42794844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:11:29 来源:igfitidea点击:
Converting pandas series timestamp to list of unique dates
提问by PyRaider
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,
我在 Pandas 数据框中有一列时间戳格式,并希望将唯一日期(无时间)提取到列表中。我试过以下方法并不奏效,
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()
can anyone help?
有人可以帮忙吗?
回答by Psidom
You can use dt
to access the date time object in a Series, try this:
您可以使用dt
访问系列中的日期时间对象,试试这个:
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"]})