Just like the string namespace, there is also a datetime namespace with specials datetime methods. In order to apply these methods, the column/Series has to have a datetime datatype.
date_strings = pd.Series(['3/11/2000', '3/12/2000', '3/13/2000'])
date_strings
0 3/11/2000
1 3/12/2000
2 3/13/2000
dtype: object
Let's convert this to a DateTime Series using pd.to_datetime(). It tries to infer the datetime format automatically:
pd.to_datetime(date_strings)
0 2000-03-11
1 2000-03-12
2 2000-03-13
dtype: datetime64[ns]
Sometimes this doesn't work correctly. You can give Pandas some extra information to correctly infer the format:
pd.to_datetime(date_strings, dayfirst=False, yearfirst=False)
0 2000-03-11
1 2000-03-12
2 2000-03-13
dtype: datetime64[ns]
Or you can give Pandas an exact format:
pd.to_datetime(date_strings, format='%m/%d/%Y')
0 2000-03-11
1 2000-03-12
2 2000-03-13
dtype: datetime64[ns]
Pandas used datetime formats as defined in the Python time module: https://docs.python.org/2/library/time.html#time.strftime
PS
datetime format is altijd YYYY-MM_DD. bij inlezen dataframe ook eraan denken dat als oorspronkelijke data een timestamp bevat dit ook opgegeven moet worden bij inlezen in datatime veld
bijvoorbeeld.
df
#Passengers | |
---|---|
Month | |
1949-01 | 112 |
1949-02 | 118 |
1949-03 | 132 |
1949-04 | 129 |
pd.to_datetime(df['Month'], format='%Y-%m')
Accesing datetime namespace
Access the datetime namespace using .dt on a Series of datetime objects:
ratings['timestamp'].dt.month.value_counts()
RESAMPLE
A
DataFrame or Series that has a datetime index can be resampled to a
desired frequency, like months, using the .resample method. This
effectively means a groupby on the months of the datetimes:
ratings_new.rating.resample('M').mean().head(10)
Geen opmerkingen:
Een reactie posten