Date/time utilities in the pandas
module are more flexible/powerful than that in the datetime
module.
It is suggested that you use date/time utilities in the pandas
module
when you use DataFrame/Series in the pandas
module.
pandas.to_datetime works on an iterable object, handles missing values and nano seconds.
pandas.Series.dt.strftime
In [4]:
import pandas as pd
?pd.to_datetime
In [6]:
pd.to_datetime(12016)
Out[6]:
In [7]:
pd.to_datetime("2017-01-01")
Out[7]:
In [12]:
date = pd.to_datetime(["2017-01-01", "2017-01-02"])
print(date)
type(date)
Out[12]:
In [13]:
date = pd.to_datetime(pd.Series(["2017-01-01", "2017-01-02"]))
print(date)
type(date)
Out[13]:
In [22]:
import pandas as pd
dt = pd.to_datetime("2017-01-18 09:21:29.123456000", format="%Y-%m-%d %H:%M:%S.%f")
dt
Out[22]:
In [17]:
import pandas as pd
import numpy as np
dt = pd.to_datetime(np.nan, format="%Y-%m-%d %H:%M:%S.%f789")
dt
Out[17]:
In [20]:
import datetime
datetime.datetime.strptime("2017-01-18 09:21:29.123456000", "%Y-%m-%d %H:%M:%S.%f")
In [19]:
import datetime
datetime.datetime.strptime(np.nan, "%Y-%m-%d %H:%M:%S.%f")
In [13]:
import pandas as pd
dt = pd.to_datetime("2017-01-18 09:21:29.123456789", format="%Y-%m-%d %H:%M:%S.%f")
dt.strftime("%Y-%m-%d %H:%M:%S.%f000")
Out[13]:
In [8]:
dir(dt)
Out[8]:
In [6]:
import pandas as pd
pd.to_datetime("2017-01-18 09:21:29.000000", format="%Y-%m-%d %H:%M:%S.%f")
Out[6]:
In [ ]: