Python 将 Pandas 系列转换为 DataFrame 中的 DateTime

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28133018/
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-08-19 02:47:05  来源:igfitidea点击:

Convert Pandas Series to DateTime in a DataFrame

pythondatetimepandasdataframe

提问by 1EnemyLeft

I have a Pandas DataFrame as below

我有一个如下所示的 Pandas DataFrame

        ReviewID       ID      Type               TimeReviewed
205     76032930  51936827  ReportID 2015-01-15 00:05:27.513000
232     76032930  51936854  ReportID 2015-01-15 00:06:46.703000
233     76032930  51936855  ReportID 2015-01-15 00:06:56.707000
413     76032930  51937035  ReportID 2015-01-15 00:14:24.957000
565     76032930  51937188  ReportID 2015-01-15 00:23:07.220000

>>> type(df)
<class 'pandas.core.frame.DataFrame'>

TimeReviewed is a series type

TimeReviewed 是一个系列类型

>>> type(df.TimeReviewed)
<class 'pandas.core.series.Series'>

I've tried below, but it still doesn't change the Series type

我在下面尝试过,但它仍然没有改变系列类型

import pandas as pd
review = pd.to_datetime(pd.Series(df.TimeReviewed))
>>> type(review)
<class 'pandas.core.series.Series'>

How can I change the df.TimeReviewed to DateTime type and pull out year, month, day, hour, min, sec separately? I'm kinda new to python, thanks for your help.

如何将 df.TimeReviewed 更改为 DateTime 类型并分别提取年、月、日、小时、分钟、秒?我对 python 有点陌生,谢谢你的帮助。

采纳答案by DSM

You can't: DataFramecolumns are Series, by definition. That said, if you make the dtype(the type of all the elements) datetime-like, then you can access the quantities you want via the .dtaccessor (docs):

您不能:根据定义,DataFrame列是Series。也就是说,如果您使dtype(所有元素的类型)类似日期时间,那么您可以通过访问.dt器(docs)访问您想要的数量:

>>> df["TimeReviewed"] = pd.to_datetime(df["TimeReviewed"])
>>> df["TimeReviewed"]
205  76032930   2015-01-24 00:05:27.513000
232  76032930   2015-01-24 00:06:46.703000
233  76032930   2015-01-24 00:06:56.707000
413  76032930   2015-01-24 00:14:24.957000
565  76032930   2015-01-24 00:23:07.220000
Name: TimeReviewed, dtype: datetime64[ns]
>>> df["TimeReviewed"].dt
<pandas.tseries.common.DatetimeProperties object at 0xb10da60c>
>>> df["TimeReviewed"].dt.year
205  76032930    2015
232  76032930    2015
233  76032930    2015
413  76032930    2015
565  76032930    2015
dtype: int64
>>> df["TimeReviewed"].dt.month
205  76032930    1
232  76032930    1
233  76032930    1
413  76032930    1
565  76032930    1
dtype: int64
>>> df["TimeReviewed"].dt.minute
205  76032930     5
232  76032930     6
233  76032930     6
413  76032930    14
565  76032930    23
dtype: int64


If you're stuck using an older version of pandas, you can always access the various elements manually (again, after converting it to a datetime-dtyped Series). It'll be slower, but sometimes that isn't an issue:

如果您坚持使用旧版本的pandas,您始终可以手动访问各种元素(再次将其转换为日期时间类型的系列之后)。它会更慢,但有时这不是问题:

>>> df["TimeReviewed"].apply(lambda x: x.year)
205  76032930    2015
232  76032930    2015
233  76032930    2015
413  76032930    2015
565  76032930    2015
Name: TimeReviewed, dtype: int64

回答by CodeFarmer

Some handy script:

一些方便的脚本:

hour = df['assess_time'].dt.hour.values[0]

回答by Shashwat Yadav

df=pd.read_csv("filename.csv" , parse_dates=["<column name>"])

type(df.<column name>)

example: if you want to convert day which is initially a string to a Timestamp in Pandas

示例:如果您想将最初是字符串的日期转换为 Pandas 中的时间戳

df=pd.read_csv("weather_data2.csv" , parse_dates=["day"])

type(df.day)

The output will be pandas.tslib.Timestamp

输出将是 pandas.tslib.Timestamp