I am running this code and on a date older then 1677 I guess there will be issue of OutOfBoundsDatetime
.
my code is
import pandas as pd
df = pd.DataFrame({'datetime_str': ['2011-01-17 23:20:00' ,'0031-01-17 23:20:00']})
df['datetime_str'] = (pd.to_datetime(df['datetime_str']).astype(int) / 10 ** 6).astype(int)
and now I want to assign the minimum date in case this error happens .and I am achieving this using this code
import pandas as pd
import numpy as np
df = pd.DataFrame({'datetime_str': ['2011-01-17 23:20:00', '0031-01-17 23:20:00']})
# convert the datetime string to epoch time
epoch_time = []
for dt_str in df['datetime_str']:
try:
epoch_time.append(int(pd.to_datetime(dt_str).timestamp()))
except pd.errors.OutOfBoundsDatetime:
epoch_time.append(int(pd.Timestamp('1970-09-21 00:12:43.145225').timestamp()))
df['epoch_time'] = epoch_time
print(df['epoch_time'])
i am able to achieve my goal but I think this is not best way to do with panda as iterating over all and I want to save epoch in milliseconds. Is there any better way ?