Ben Chuanlong Du's Blog

It is never too late to learn.

Date and Time in Python pandas

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.

  1. pandas.to_datetime works on an iterable object, handles missing values and nano seconds.

  2. pandas.Series.dt.strftime

In [4]:
import pandas as pd

?pd.to_datetime
In [6]:
pd.to_datetime(12016)
Out[6]:
Timestamp('1970-01-01 00:00:00.000012016')
In [7]:
pd.to_datetime("2017-01-01")
Out[7]:
Timestamp('2017-01-01 00:00:00')
In [12]:
date = pd.to_datetime(["2017-01-01", "2017-01-02"])
print(date)
type(date)
DatetimeIndex(['2017-01-01', '2017-01-02'], dtype='datetime64[ns]', freq=None)
Out[12]:
pandas.core.indexes.datetimes.DatetimeIndex
In [13]:
date = pd.to_datetime(pd.Series(["2017-01-01", "2017-01-02"]))
print(date)
type(date)
0   2017-01-01
1   2017-01-02
dtype: datetime64[ns]
Out[13]:
pandas.core.series.Series
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]:
Timestamp('2017-01-18 09:21:29.123456')
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]:
NaT
In [20]:
import datetime

datetime.datetime.strptime("2017-01-18 09:21:29.123456000", "%Y-%m-%d %H:%M:%S.%f")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-20-fc61fefca196> in <module>()
      1 import datetime
      2 
----> 3 datetime.datetime.strptime('2017-01-18 09:21:29.123456000', '%Y-%m-%d %H:%M:%S.%f')

/usr/lib/python3.5/_strptime.py in _strptime_datetime(cls, data_string, format)
    508     """Return a class cls instance based on the input string and the
    509     format string."""
--> 510     tt, fraction = _strptime(data_string, format)
    511     tzname, gmtoff = tt[-2:]
    512     args = tt[:6] + (fraction,)

/usr/lib/python3.5/_strptime.py in _strptime(data_string, format)
    344     if len(data_string) != found.end():
    345         raise ValueError("unconverted data remains: %s" %
--> 346                           data_string[found.end():])
    347 
    348     year = None

ValueError: unconverted data remains: 000
In [19]:
import datetime

datetime.datetime.strptime(np.nan, "%Y-%m-%d %H:%M:%S.%f")
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-19-89cd5d40393d> in <module>()
      1 import datetime
      2 
----> 3 datetime.datetime.strptime(np.nan, '%Y-%m-%d %H:%M:%S.%f')

TypeError: strptime() argument 1 must be str, not float
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]:
'2017-01-18 09:21:29.123456000'
In [8]:
dir(dt)
Out[8]:
['__add__',
 '__class__',
 '__delattr__',
 '__dict__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__gt__',
 '__hash__',
 '__init__',
 '__le__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__pyx_vtable__',
 '__radd__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__rsub__',
 '__setattr__',
 '__setstate__',
 '__sizeof__',
 '__str__',
 '__sub__',
 '__subclasshook__',
 '__weakref__',
 '_date_repr',
 '_get_field',
 '_get_start_end_field',
 '_has_time_component',
 '_repr_base',
 '_round',
 '_short_repr',
 '_time_repr',
 'asm8',
 'astimezone',
 'ceil',
 'combine',
 'ctime',
 'date',
 'day',
 'dayofweek',
 'dayofyear',
 'days_in_month',
 'daysinmonth',
 'dst',
 'floor',
 'freq',
 'freqstr',
 'fromordinal',
 'fromtimestamp',
 'hour',
 'is_leap_year',
 'is_month_end',
 'is_month_start',
 'is_quarter_end',
 'is_quarter_start',
 'is_year_end',
 'is_year_start',
 'isocalendar',
 'isoformat',
 'isoweekday',
 'max',
 'microsecond',
 'min',
 'minute',
 'month',
 'nanosecond',
 'normalize',
 'now',
 'offset',
 'quarter',
 'replace',
 'resolution',
 'round',
 'second',
 'strftime',
 'strptime',
 'time',
 'timestamp',
 'timetuple',
 'timetz',
 'to_datetime',
 'to_datetime64',
 'to_julian_date',
 'to_period',
 'to_pydatetime',
 'today',
 'toordinal',
 'tz',
 'tz_convert',
 'tz_localize',
 'tzinfo',
 'tzname',
 'utcfromtimestamp',
 'utcnow',
 'utcoffset',
 'utctimetuple',
 'value',
 'week',
 'weekday',
 'weekday_name',
 'weekofyear',
 'year']
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]:
Timestamp('2017-01-18 09:21:29')
In [ ]:
 

Comments