Function_to_date_time

In [1]:

import pandas as pd
dates = ['2017-01-05', 'Jan 5, 2017', '01/05/2017', '2017.01.05', '2017/01/05','20170105']
pd.to_datetime(dates)

Out[1]:

DatetimeIndex(['2017-01-05', '2017-01-05', '2017-01-05', '2017-01-05',
               '2017-01-05', '2017-01-05'],
              dtype='datetime64[ns]', freq=None)

In [2]:

dt = ['2017-01-05 2:30:00 PM', 'Jan 5, 2017 14:30:00', '01/05/2016', '2017.01.05', '2017/01/05','20170105']
pd.to_datetime(dt)

Out[2]:

DatetimeIndex(['2017-01-05 14:30:00', '2017-01-05 14:30:00',
               '2016-01-05 00:00:00', '2017-01-05 00:00:00',
               '2017-01-05 00:00:00', '2017-01-05 00:00:00'],
              dtype='datetime64[ns]', freq=None)

European style dates with day first

  • US: mm/dd/yy

  • Eur: dd/mm/yy

In [6]:

pd.to_datetime('01-07-2019')

Out[6]:

Timestamp('2019-01-07 00:00:00')

In [8]:

pd.to_datetime('01-07-2019', dayfirst=True)

Out[8]:

Timestamp('2019-07-01 00:00:00')

In [9]:

pd.to_datetime('2019$01$07', format='%Y$%m$%d')

Out[9]:

Timestamp('2019-01-07 00:00:00')

In [10]:

pd.to_datetime('2019#01#07', format='%Y#%m#%d')

Out[10]:

Timestamp('2019-01-07 00:00:00')

In [13]:

# Leave the invalid values (‘abc’)
pd.to_datetime(['2017-01-05', 'Jan 6, 2017', 'abc'], errors='ignore')

Out[13]:

Index(['2017-01-05', 'Jan 6, 2017', 'abc'], dtype='object')

In [14]:

# Assign NaT type for invalid values
pd.to_datetime(['2017-01-05', 'Jan 6, 2017', 'abc'], errors='coerce')

Out[14]:

DatetimeIndex(['2017-01-05', '2017-01-06', 'NaT'], dtype='datetime64[ns]', freq=None)

Epoch or Unix time means number of seconds that have passed since Jan 1, 1970 00:00:00 UTC time

In [21]:

# Search Online
current_epoch = 1563979818
pd.to_datetime(current_epoch, unit='s')

Out[21]:

Timestamp('2019-07-24 14:50:18')

In [22]:

pd.to_datetime(current_epoch*1000, unit='ms')

Out[22]:

Timestamp('2019-07-24 14:50:18')

In [25]:

# change format from tuple to list, if not can't convert it back
t = pd.to_datetime([current_epoch], unit='s')
t

Out[25]:

DatetimeIndex(['2019-07-24 14:50:18'], dtype='datetime64[ns]', freq=None)

Out[26]:

array([1563979818000000000], dtype=int64)

Last updated