I have a Pandas Series ("timeSeries") that includes a time of day. Some of the items are blank, some are actual times (08:00; 13:00), some are indications of time (morning, early afternoon).
As the time of day I have is New York, I would like to convert the items in the time format to London time. Using pd.to_datetime(timeSeries, error='ignore')
does not work when I also have the addition of timedelta(hours=5)
. So I attempted to add a if condition but it does not work.
Sample Initial DataFrame:
dfNY = pd.DataFrame({'TimeSeries': [13:00, nan, 06:00, 'Morning', 'Afternoon', nan, nan, 01:30])
Desired Result:
dfLondon = pd.DataFrame({'TimeSeries': [18:00, nan, 11:00, 'Morning', 'Afternoon', nan, nan, 06:30])
Any help or simplification of my code would be great.
london = dt.datetime.now(timezone("America/New_York"))
newYork = dt.datetime.now(timezone("Europe/London"))
timeDiff = (london - dt.timedelta(hours = newYork.hour)).hour
for dayTime in timeSeries:
if dayTime == "%%:%%":
print(dayTime)
dayTime = pd.to_datetime(dayTime) + dt.timedelta(hours=timeDiff)
return timeSeries
Update: using pytz method in comment below yields a timezone that is off my 5min. How do we fix this?