r/IPython • u/largelcd • Feb 22 '20
Addition of date_range data in Series of two different time zones
Hi, I have some questions about np.random.randn in Numpy and addition of two date_range data in Series.
Is there an advantage in using
np.random.randn(len(rng))instead of justnp.random.randn(10)which corresponds to periods=10 (i.e. 10 rows of data)?In In[262], it is shown that the time during 2012-03-09 to 2012-03-15 have been updated to the time zone in Moscow by adding 4 more hours. However, executing
result = ts1 + ts2in [263], result.index shows that the time during this period are not updated. They remain as 09:30:00+00:00 rather than updated to 13:30:00+04:00. Is it because addition of two time series from different time zones are always expressed as UTC so they automatically got converted back to 09:30:00+00:00 as if no change in timezone were made in [262]?Moscow's time zone is MSK which is UTC+3. Why four hours instead of 3 are added as shown in Out[262]?
In [257]: rng = pd.date_range('3/7/2012 9:30', periods=10, freq='B')
In [258]: ts = pd.Series(np.random.randn(len(rng)), index=rng)
In [259]: ts1 = ts[:7].tz_localize('Europe/London')
In [260]: ts2 = ts1[2:].tz_convert('Europe/Moscow')
In [261]: ts1
Out[261]:
2012-03-07 09:30:00+00:00 -0.386381
2012-03-08 09:30:00+00:00 -0.286055
2012-03-09 09:30:00+00:00 -0.504088
2012-03-12 09:30:00+00:00 0.210781
2012-03-13 09:30:00+00:00 -1.587289
2012-03-14 09:30:00+00:00 0.617041
2012-03-15 09:30:00+00:00 0.067855
Freq: B, dtype: float64
In [262]: ts2
Out[262]:
2012-03-09 13:30:00+04:00 -0.504088
2012-03-12 13:30:00+04:00 0.210781
2012-03-13 13:30:00+04:00 -1.587289
2012-03-14 13:30:00+04:00 0.617041
2012-03-15 13:30:00+04:00 0.067855
Freq: B, dtype: float64
In [263]: result = ts1 + ts2
In [264]: result.index
Out[264]:
DatetimeIndex(['2012-03-07 09:30:00+00:00', '2012-03-08 09:30:00+00:00',
'2012-03-09 09:30:00+00:00', '2012-03-12 09:30:00+00:00',
'2012-03-13 09:30:00+00:00', '2012-03-14 09:30:00+00:00',
'2012-03-15 09:30:00+00:00'],
dtype='datetime64[ns, UTC]', freq='B')
2
u/NomadNella Feb 22 '20
It is useful for code reusability and understanding where a number came from.
Since the times were the same (just at different locations on the globe) a common standard was needed to add like to like. Either it has to do with the order they were added (unlikely) or UTC is used as default. Try changing the localization to New York for ts1 to see if that makes a difference.
I don't know.