2

I am trying to create timezone aware date column in a pandas DataFrame. When I run the code below, the resulting pandas column does not have the same datetime as the one I inputted. What am I doing wrong here?

I am using python 3.6.2 and pandas 0.20.3

from datetime import datetime
import pandas as pd
import pytz 
date_string = "12/14/2016 12:00"
timezone = pytz.timezone("US/Pacific")
input_datetime =  datetime.strptime(date_string, "%m/%d/%Y %H:%M").replace(tzinfo=timezone)
df = pd.DataFrame({"datetime":[input_datetime]})

If I run that code, df['datetime'][0].minute returns 53 while input_datetime.minute returns 0.

When I don't replace the tzinfo I do not have a problem.

Joseph K.
  • 1,055
  • 3
  • 23
  • 46
jackalack
  • 585
  • 4
  • 13
  • 4
    Post your actual code, not a screenshot. Also take a look at [this link](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). – pault Dec 14 '17 at 21:25
  • What do you mean by changing? Is it because you are calling minutes of `x` which is `10/5/16 0:00`? – Joseph K. Dec 14 '17 at 21:30
  • @JosephK. `datetime.datetime(2016, 12, 14, 12, 0, tzinfo=)` becomes `Timestamp('2016-12-14 11:53:00-0800', tz='US/Pacific')` after I create the pandas DataFrame – jackalack Dec 14 '17 at 23:49

2 Answers2

1

If you first convert your input_datetime you can call the minutes (or years etc) of your dataframe with .dt.minute

input_datetime = pd.to_datetime(datetime.strptime(date_string, 
                                                  "%m/%d/%Y %H:%M")).replace(tzinfo=timezone)


df = pd.DataFrame({"datetime":[input_datetime]})
df['datetime'].dt.minute
ohe
  • 3,461
  • 3
  • 26
  • 50
Chiel
  • 1,865
  • 1
  • 11
  • 24
0

You can use pandas .dt and tz_localize:

from datetime import datetime
import pandas as pd
date_string = "12/14/2016 12:00"
df = pd.DataFrame({'datetime':[datetime.strptime(date_string, "%m/%d/%Y %H:%M")]})
df['datetime'].dt.tz_localize('US/Pacific')

Output:

0   2016-12-14 12:00:00-08:00
Name: datetime, dtype: datetime64[ns, US/Pacific]
Scott Boston
  • 147,308
  • 15
  • 139
  • 187