I have a Pandas DataFrame as below
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
>>> 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.
to_datetime() Function helps in converting a date string to a python date object. So, it can be utilized for converting a series of date strings to a time series.
Use pandas to_datetime() function to convert the column to DateTime on DataFrame. Use the format parameter of this method to specify the pattern of the DateTime string you wanted to convert.
You can't: DataFrame
columns 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 .dt
accessor (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:
>>> 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With