将 Pandas 系列转换为 DataFrame 中的日期时间
Convert Pandas Series to DateTime in a DataFrame
我有一个 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 是系列类型
>>> type(df.TimeReviewed)
<class 'pandas.core.series.Series'>
我试过下面,但它仍然没有改变系列类型
import pandas as pd
review = pd.to_datetime(pd.Series(df.TimeReviewed))
>>> type(review)
<class 'pandas.core.series.Series'>
如何把df.TimeReviewed改成DateTime类型,分别拉出年、月、日、时、分、秒?
我是 python 的新手,感谢您的帮助。
您不能:根据定义,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
如果您无法使用旧版本的 pandas
,您始终可以手动访问各种元素(同样,在将其转换为 datetime-dtyped 系列之后)。它会更慢,但有时这不是问题:
>>> 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
一些方便的脚本:
hour = df['assess_time'].dt.hour.values[0]
df=pd.read_csv("filename.csv" , parse_dates=["<column name>"])
type(df.<column name>)
示例:如果您想将最初为字符串的日期转换为 Pandas
中的时间戳
df=pd.read_csv("weather_data2.csv" , parse_dates=["day"])
type(df.day)
输出将是pandas.tslib.Timestamp
我有一个 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 是系列类型
>>> type(df.TimeReviewed)
<class 'pandas.core.series.Series'>
我试过下面,但它仍然没有改变系列类型
import pandas as pd
review = pd.to_datetime(pd.Series(df.TimeReviewed))
>>> type(review)
<class 'pandas.core.series.Series'>
如何把df.TimeReviewed改成DateTime类型,分别拉出年、月、日、时、分、秒? 我是 python 的新手,感谢您的帮助。
您不能:根据定义,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
如果您无法使用旧版本的 pandas
,您始终可以手动访问各种元素(同样,在将其转换为 datetime-dtyped 系列之后)。它会更慢,但有时这不是问题:
>>> 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
一些方便的脚本:
hour = df['assess_time'].dt.hour.values[0]
df=pd.read_csv("filename.csv" , parse_dates=["<column name>"])
type(df.<column name>)
示例:如果您想将最初为字符串的日期转换为 Pandas
中的时间戳df=pd.read_csv("weather_data2.csv" , parse_dates=["day"])
type(df.day)
输出将是pandas.tslib.Timestamp