-1

I am trying to convert the timezone of a pandas Series. I am using the pytz package to do so, however I am getting a value that is off by a few minutes. The code I am currently using can be found in the answer here: Converting Items from Pandas Series to Date Time

Other answers suggest using the localize() function to make this work, however this does not work in my code. I tried using the normalize() function to solve this issue but I am getting an error using a pandas series.

Error message: AttributeError: 'Series' object has no attribute 'tzinfo'

Sample input:

dfNY = pd.DataFrame({'TimeSeries': [13:00, nan, 06:00, 'Morning', 'Afternoon', nan, nan, 01:30]})

Desired output:

dfLondon = pd.DataFrame({'TimeSeries': [18:00, nan, 11:00, 'Morning', 'Afternoon', nan, nan, 06:30]})

Current code:

    import pandas as pd
    from pytz import timezone
    dfNY = pd.DataFrame({'TimeSeries': [13:00, nan, 06:00, 'Morning', 'Afternoon', nan, nan, 01:30]})
    tzDestination = "Europe/London"
    dtTimeSeries = pd.to_datetime(dfNY.TimeSeries, errors='coerce', format='%H:%M').dt.tz_localize(tzOrigin)
    tzChange = timezone(tzDestination)
    convertedTime = tzChange.normalize(dtTimeSeries).dt.strftime('%H:%M')
    dyNY = convertedTime.copy()
    dfNY = timeSeries.where(~convertedTime.ne('NaT'), convertedTime)

Thanks for any help

ohoh7171
  • 186
  • 2
  • 2
  • 14
  • could you show the code you are using too? Adding the error message without the code producing it is not really useful. – Valentino Oct 01 '19 at 12:29
  • Possible duplicate of [Change utcoffset of a Timestamp](https://stackoverflow.com/questions/41304394/change-utcoffset-of-a-timestamp) – tnknepp Oct 01 '19 at 13:56

1 Answers1

0

When I run the code from the previous answer it works just fine. Compare your code to IMCoins' and see where the difference is (you create a separate time series whereas IMCoins keeps everything within the dataframe).

Second, regarding why the time stamp is off by four minutes, see the answer here. How can you fix this? Well, I doubt you have data from the year 1900, so you could set the date as something AFTER 1-Jan. 1902.

tnknepp
  • 5,888
  • 6
  • 43
  • 57
  • If you look at the output of the previous answer - he is off by 5min as well. I find that on different machines, the output differs The issue with the answer you provided is that it does not work with pandas Series. I localize the data in the series dtTimeSeries – ohoh7171 Oct 01 '19 at 14:25
  • Sorry, I do not see any differences amongst the answers. The offset is consistently off by 4 minutes. As shown in the linked answer (second link) this offset is not wrong, it is due to how time was recorded in the early 20th century (why four minutes matters, I don't know...it's just the way its done, so no bug here). – tnknepp Oct 01 '19 at 15:02
  • 1. answer in other question does not solve the minutes issue 2. I am unable to get the desired result regardless of using the localize method 3. I still get an error with the pandas data-series if I try to use the "normalize" method – ohoh7171 Oct 02 '19 at 09:00