0

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 ?

Ninja
  • 241
  • 2
  • 3
  • 13
  • "and now I want to assign the minimum date" would this just be 0 (since 0 is the minimum date since the epoch?!) – cs95 Apr 12 '23 at 19:25
  • @cs95 I will rather show some date . I did some search and found 1667 is minimum date padans can process so I will show that date – Ninja Apr 13 '23 at 06:40

0 Answers0