Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!
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
import pandas as pd
?pd.to_datetimepd.to_datetime(12016)Timestamp('1970-01-01 00:00:00.000012016')pd.to_datetime("2017-01-01")Timestamp('2017-01-01 00:00:00')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)
pandas.core.indexes.datetimes.DatetimeIndexdate = 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]
pandas.core.series.Seriesimport pandas as pd
dt = pd.to_datetime("2017-01-18 09:21:29.123456000", format="%Y-%m-%d %H:%M:%S.%f")
dtTimestamp('2017-01-18 09:21:29.123456')import pandas as pd
import numpy as np
dt = pd.to_datetime(np.nan, format="%Y-%m-%d %H:%M:%S.%f789")
dtNaTimport 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: 000import 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 floatimport 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")'2017-01-18 09:21:29.123456000'dir(dt)['__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']import pandas as pd
pd.to_datetime("2017-01-18 09:21:29.000000", format="%Y-%m-%d %H:%M:%S.%f")Timestamp('2017-01-18 09:21:29')