In [1]:
import pandas as pd

In [2]:
s = '2018-01-01T12:00+09:00'
print(s)


2018-01-01T12:00+09:00

In [3]:
print(type(s))


<class 'str'>

In [4]:
ts = pd.to_datetime(s)
print(ts)


2018-01-01 12:00:00+09:00

In [5]:
print(type(ts))


<class 'pandas._libs.tslibs.timestamps.Timestamp'>

In [6]:
print(ts.tz)


pytz.FixedOffset(540)

In [7]:
ts_utc = pd.to_datetime(s, utc=True)
print(ts_utc)


2018-01-01 03:00:00+00:00

In [8]:
print(ts_utc.tz)


UTC

In [9]:
s_without_tz = '2018-01-01T12:00'

In [10]:
ts_naive = pd.to_datetime(s_without_tz)
print(ts_naive)


2018-01-01 12:00:00

In [11]:
print(ts_naive.tz)


None

In [12]:
ts_set_utc = pd.to_datetime(s_without_tz, utc=True)
print(ts_set_utc)


2018-01-01 12:00:00+00:00

In [13]:
print(ts_set_utc.tz)


UTC

In [14]:
print(ts_utc)


2018-01-01 03:00:00+00:00

In [15]:
print(ts_utc.tz)


UTC

In [16]:
ts_jst = ts_utc.tz_convert('Asia/Tokyo')
print(ts_jst)


2018-01-01 12:00:00+09:00

In [17]:
print(ts_jst.tz)


Asia/Tokyo

In [18]:
print(ts_utc.value)


1514775600000000000

In [19]:
print(ts_jst.value)


1514775600000000000

In [20]:
print(ts_utc == ts_jst)


True

In [21]:
ts_pst = ts_utc.tz_convert('US/Pacific')
print(ts_pst)


2017-12-31 19:00:00-08:00

In [22]:
print(ts_pst.tz)


US/Pacific

In [23]:
print(ts_utc.tz_convert('America/Los_Angeles'))


2017-12-31 19:00:00-08:00

In [24]:
print(ts_utc.tz_convert('America/Vancouver'))


2017-12-31 19:00:00-08:00

In [25]:
print(ts_naive)


2018-01-01 12:00:00

In [26]:
print(ts_naive.tz)


None

In [27]:
# print(ts_naive.tz_convert('Asia/Tokyo'))
# TypeError: Cannot convert tz-naive Timestamp, use tz_localize to localize

In [28]:
ts_jst_localize = ts_naive.tz_localize('Asia/Tokyo')
print(ts_jst_localize)


2018-01-01 12:00:00+09:00

In [29]:
print(ts_jst_localize.tz)


Asia/Tokyo

In [30]:
print(ts_naive.tz_localize('US/Pacific'))


2018-01-01 12:00:00-08:00

In [31]:
print(ts_naive.tz_localize('Asia/Tokyo') == ts_naive.tz_localize('US/Pacific'))


False

In [32]:
print(ts_jst)


2018-01-01 12:00:00+09:00

In [33]:
print(ts_jst.tz)


Asia/Tokyo

In [34]:
# print(ts_jst.tz_localize('US/Pacific'))
# TypeError: Cannot localize tz-aware Timestamp, use tz_convert for conversions

In [35]:
print(ts_jst)


2018-01-01 12:00:00+09:00

In [36]:
print(ts_jst.tz)


Asia/Tokyo

In [37]:
print(ts_jst.tz_convert(None))


2018-01-01 03:00:00

In [38]:
print(ts_jst.tz_localize(None))


2018-01-01 12:00:00

In [39]:
df = pd.DataFrame({'date': ['2018-01-01T12:00',
                            '2018-01-02T00:00',
                            '2018-01-03T10:00',
                            '2018-01-03T19:00'],
                   'value': ['A', 'B', 'C', 'D']})

In [40]:
print(df)


               date value
0  2018-01-01T12:00     A
1  2018-01-02T00:00     B
2  2018-01-03T10:00     C
3  2018-01-03T19:00     D

In [41]:
s_naive = pd.to_datetime(df['date'])
print(s_naive)


0   2018-01-01 12:00:00
1   2018-01-02 00:00:00
2   2018-01-03 10:00:00
3   2018-01-03 19:00:00
Name: date, dtype: datetime64[ns]

In [42]:
print(s_naive[0])


2018-01-01 12:00:00

In [43]:
print(type(s_naive[0]))


<class 'pandas._libs.tslibs.timestamps.Timestamp'>

In [44]:
print(s_naive[0].tz)


None

In [45]:
s_utc = pd.to_datetime(df['date'], utc=True)
print(s_utc)


0   2018-01-01 12:00:00+00:00
1   2018-01-02 00:00:00+00:00
2   2018-01-03 10:00:00+00:00
3   2018-01-03 19:00:00+00:00
Name: date, dtype: datetime64[ns, UTC]

In [46]:
print(s_utc[0].tz)


UTC

In [47]:
# print(s_naive.tz_localize('Asia/Tokyo'))
# TypeError: index is not a valid DatetimeIndex or PeriodIndex

In [48]:
# print(s_utc.tz_convert('Asia/Tokyo'))
# TypeError: index is not a valid DatetimeIndex or PeriodIndex

In [49]:
print(s_naive.dt.tz_localize('Asia/Tokyo'))


0   2018-01-01 12:00:00+09:00
1   2018-01-02 00:00:00+09:00
2   2018-01-03 10:00:00+09:00
3   2018-01-03 19:00:00+09:00
Name: date, dtype: datetime64[ns, Asia/Tokyo]

In [50]:
print(s_utc.dt.tz_convert('Asia/Tokyo'))


0   2018-01-01 21:00:00+09:00
1   2018-01-02 09:00:00+09:00
2   2018-01-03 19:00:00+09:00
3   2018-01-04 04:00:00+09:00
Name: date, dtype: datetime64[ns, Asia/Tokyo]

In [51]:
# print(s_naive.dt.tz_convert('Asia/Tokyo'))
# TypeError: Cannot convert tz-naive timestamps, use tz_localize to localize

In [52]:
# print(s_utc.dt.tz_localize('Asia/Tokyo'))
# TypeError: Already tz-aware, use tz_convert to convert.

In [53]:
# print(df['date'].dt.tz_localize('Asia/Tokyo'))
# AttributeError: Can only use .dt accessor with datetimelike values

In [54]:
df['date'] = pd.to_datetime(df['date'])
df_ts = df.set_index('date')
print(df_ts)


                    value
date                     
2018-01-01 12:00:00     A
2018-01-02 00:00:00     B
2018-01-03 10:00:00     C
2018-01-03 19:00:00     D

In [55]:
print(df_ts.index)


DatetimeIndex(['2018-01-01 12:00:00', '2018-01-02 00:00:00',
               '2018-01-03 10:00:00', '2018-01-03 19:00:00'],
              dtype='datetime64[ns]', name='date', freq=None)

In [56]:
print(type(df_ts.index))


<class 'pandas.core.indexes.datetimes.DatetimeIndex'>

In [57]:
print(df_ts['2018-01-03'])


                    value
date                     
2018-01-03 10:00:00     C
2018-01-03 19:00:00     D

In [58]:
print(df_ts.index.tz_localize('Asia/Tokyo'))


DatetimeIndex(['2018-01-01 12:00:00+09:00', '2018-01-02 00:00:00+09:00',
               '2018-01-03 10:00:00+09:00', '2018-01-03 19:00:00+09:00'],
              dtype='datetime64[ns, Asia/Tokyo]', name='date', freq=None)

In [59]:
print(df_ts.tz_localize('Asia/Tokyo'))


                          value
date                           
2018-01-01 12:00:00+09:00     A
2018-01-02 00:00:00+09:00     B
2018-01-03 10:00:00+09:00     C
2018-01-03 19:00:00+09:00     D

In [60]:
s_ts = df_ts['value']
print(s_ts)


date
2018-01-01 12:00:00    A
2018-01-02 00:00:00    B
2018-01-03 10:00:00    C
2018-01-03 19:00:00    D
Name: value, dtype: object

In [61]:
print(s_ts.tz_localize('Asia/Tokyo'))


date
2018-01-01 12:00:00+09:00    A
2018-01-02 00:00:00+09:00    B
2018-01-03 10:00:00+09:00    C
2018-01-03 19:00:00+09:00    D
Name: value, dtype: object

In [62]:
df = pd.DataFrame({'date': ['2018-01-01T12:00+09:00',
                            '2018-01-02T00:00+09:00',
                            '2018-01-03T10:00+09:00',
                            '2018-01-03T19:00+09:00'],
                   'value': ['A', 'B', 'C', 'D']})

In [63]:
print(df)


                     date value
0  2018-01-01T12:00+09:00     A
1  2018-01-02T00:00+09:00     B
2  2018-01-03T10:00+09:00     C
3  2018-01-03T19:00+09:00     D

In [64]:
print(pd.to_datetime(df['date']))


0   2018-01-01 12:00:00+09:00
1   2018-01-02 00:00:00+09:00
2   2018-01-03 10:00:00+09:00
3   2018-01-03 19:00:00+09:00
Name: date, dtype: datetime64[ns, pytz.FixedOffset(540)]

In [65]:
print(pd.to_datetime(df['date'], utc=True))


0   2018-01-01 03:00:00+00:00
1   2018-01-01 15:00:00+00:00
2   2018-01-03 01:00:00+00:00
3   2018-01-03 10:00:00+00:00
Name: date, dtype: datetime64[ns, UTC]

In [66]:
print(pd.to_datetime(df['date']).dt.tz_convert('US/Pacific'))


0   2017-12-31 19:00:00-08:00
1   2018-01-01 07:00:00-08:00
2   2018-01-02 17:00:00-08:00
3   2018-01-03 02:00:00-08:00
Name: date, dtype: datetime64[ns, US/Pacific]

In [67]:
df['date'] = pd.to_datetime(df['date'])
df_ts = df.set_index('date')
print(df_ts)


                          value
date                           
2018-01-01 12:00:00+09:00     A
2018-01-02 00:00:00+09:00     B
2018-01-03 10:00:00+09:00     C
2018-01-03 19:00:00+09:00     D

In [68]:
print(df_ts.index)


DatetimeIndex(['2018-01-01 12:00:00+09:00', '2018-01-02 00:00:00+09:00',
               '2018-01-03 10:00:00+09:00', '2018-01-03 19:00:00+09:00'],
              dtype='datetime64[ns, pytz.FixedOffset(540)]', name='date', freq=None)

In [69]:
print(df_ts.tz_convert('US/Pacific'))


                          value
date                           
2017-12-31 19:00:00-08:00     A
2018-01-01 07:00:00-08:00     B
2018-01-02 17:00:00-08:00     C
2018-01-03 02:00:00-08:00     D

In [70]:
df = pd.DataFrame({'date': ['2018-01-01T12:00+09:00',
                            '2018-01-02T00:00+09:00',
                            '2018-01-03T10:00-05:00',
                            '2018-01-03T19:00-08:00'],
                   'value': ['A', 'B', 'C', 'D']})

In [71]:
print(df)


                     date value
0  2018-01-01T12:00+09:00     A
1  2018-01-02T00:00+09:00     B
2  2018-01-03T10:00-05:00     C
3  2018-01-03T19:00-08:00     D

In [72]:
print(pd.to_datetime(df['date']))


0    2018-01-01 12:00:00+09:00
1    2018-01-02 00:00:00+09:00
2    2018-01-03 10:00:00-05:00
3    2018-01-03 19:00:00-08:00
Name: date, dtype: object

In [73]:
print(type(pd.to_datetime(df['date'])[0]))


<class 'datetime.datetime'>

In [74]:
print(pd.to_datetime(df['date'])[0].tzinfo)


tzoffset(None, 32400)

In [75]:
print(pd.to_datetime(df['date'])[2].tzinfo)


tzoffset(None, -18000)

In [76]:
print(pd.to_datetime(df['date'], utc=True))


0   2018-01-01 03:00:00+00:00
1   2018-01-01 15:00:00+00:00
2   2018-01-03 15:00:00+00:00
3   2018-01-04 03:00:00+00:00
Name: date, dtype: datetime64[ns, UTC]

In [77]:
print(type(pd.to_datetime(df['date'], utc=True)[0]))


<class 'pandas._libs.tslibs.timestamps.Timestamp'>

In [78]:
# print(pd.to_datetime(df['date']).dt.tz_convert('Asia/Tokyo'))
# ValueError: Tz-aware datetime.datetime cannot be converted to datetime64 unless utc=True

In [79]:
df['date'] = pd.to_datetime(df['date'])
df_dt = df.set_index('date')
print(df_dt)


                          value
date                           
2018-01-01 12:00:00+09:00     A
2018-01-02 00:00:00+09:00     B
2018-01-03 10:00:00-05:00     C
2018-01-03 19:00:00-08:00     D

In [80]:
print(df_dt.index)


Index([2018-01-01 12:00:00+09:00, 2018-01-02 00:00:00+09:00,
       2018-01-03 10:00:00-05:00, 2018-01-03 19:00:00-08:00],
      dtype='object', name='date')

In [81]:
# print(df_dt.tz_convert('Asia/Tokyo'))
# TypeError: index is not a valid DatetimeIndex or PeriodIndex

In [82]:
# print(df_dt.tz_localize('Asia/Tokyo'))
# TypeError: index is not a valid DatetimeIndex or PeriodIndex