我有一个如下的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'>
我已经在下面尝试过,但是它仍然没有改变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)访问所需的数量:
DataFrame
Series
dtype
.dt
>>> 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系列后)。它会变慢,但有时这不是问题:
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