maandag 6 mei 2019

Datetime

Datetime Namespace

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:

Geen opmerkingen:

Een reactie posten

Datums bepalen adhv begin en einddatum in Dataframe

Voorbeeld op losse velden  ####################################################################### # import necessary packages from datetime...