1

I have a Pandas DatetimeIndex object (obtained from the index of a dataframe) and I need the weekday of each element. I have been running this with Python 3.6.5 (64-bit) on Windows and with Pandas 0.22.0 and obtaining an Int64Index object:

e.g.

In:
    ts = pd.DatetimeIndex([['2013-01-01 00:00:00', '2013-01-01 00:30:00',
               '2013-01-02 01:00:00', '2013-01-02 01:30:00',
               '2013-01-03 02:00:00', '2013-01-03 02:30:00',
               '2013-01-04 03:00:00', '2013-01-04 03:30:00',
               '2013-01-05 04:00:00', '2013-01-05 04:30:00']])
    ts.weekday

Out:
    Int64Index([1, 1, 2, 2, 3, 3, 4, 4, 5, 5], dtype='int64')

I am now running the script on pythonanywhere which has Python 3.6.0 and Pandas 0.19.2 and the result is returned as an array:

In:        
    ts = pd.DatetimeIndex(['2013-01-01 00:00:00', '2013-01-01 00:30:00',
                    '2013-01-02 01:00:00', '2013-01-02 01:30:00',
                    '2013-01-03 02:00:00', '2013-01-03 02:30:00',
                    '2013-01-04 03:00:00', '2013-01-04 03:30:00',
                    '2013-01-05 04:00:00', '2013-01-05 04:30:00'])
        ts.weekday
    Out:
        array([1, 1, 2, 2, 3, 3, 4, 4, 5, 5], dtype=int32)

Is it possible to write this code to return the same result (ideally an Index object) on both platforms?

cs95
  • 379,657
  • 97
  • 704
  • 746
doctorer
  • 1,672
  • 5
  • 27
  • 50

1 Answers1

2

This behaviour was changed in a recent update to return a pd.Index instead of an array. For backwards compatibility, you can always cast the result to an Int64Index as needed.

>>> pd.Index(ts.weekday, dtype=np.int64)
Int64Index([1, 1, 2, 2, 3, 3, 4, 4, 5, 5], dtype='int64')
cs95
  • 379,657
  • 97
  • 704
  • 746