BUG/API: Make consistent API for tz-aware and tz-naive ops · Issue #11351 · pandas-dev/pandas (original) (raw)
Currently there are some inconsistent results. We should clarify the policy first.
import pandas as pd
s = pd.Series([pd.Timestamp('2011-01-01'), pd.NaT])
s
#0 2011-01-01
#1 NaT
# dtype: datetime64[ns]
1. coerced to object dtype
I prefer this behavior.
s.fillna(pd.Timestamp('2011-01-02', tz='Asia/Tokyo'))
#0 2011-01-01 00:00:00
#1 2011-01-02 00:00:00+09:00
# dtype: object
2. converted to GMT
s[1] = pd.Timestamp('2011-01-02', tz='Asia/Tokyo')
s
#0 2011-01-01 00:00:00
#1 2011-01-01 15:00:00
# dtype: datetime64[ns]
3. raise explicit error
This looks second best.
stz = pd.Series(pd.DatetimeIndex(['2012-01-01', '2012-01-02'], tz='Asia/Tokyo'))
stz
#0 2012-01-01 00:00:00+09:00
#1 2012-01-02 00:00:00+09:00
# dtype: datetime64[ns, Asia/Tokyo]
pd.Index(s).union(pd.Index(stz))
# TypeError: Cannot join tz-naive with tz-aware DatetimeIndex
4. not handled properly (bug)
s.append(stz)
# AttributeError: 'numpy.ndarray' object has no attribute 'tz_localize'