0

Even though the single-level index of my dataframe shown below is clearly a pd.DatetimeIndex, it is still also identified as a pd.Int64Index.

In what follows, I demonstrate this behavior via printouts in the VS Code debugging console:

df.index
DatetimeIndex(['2018-01-01 00:00:00+01:00', '2018-01-01 01:00:00+01:00',
               '2018-01-01 02:00:00+01:00', '2018-01-01 03:00:00+01:00',
               '2018-01-01 04:00:00+01:00', '2018-01-01 05:00:00+01:00',
               '2018-01-01 06:00:00+01:00', '2018-01-01 07:00:00+01:00',
               '2018-01-01 08:00:00+01:00', '2018-01-01 09:00:00+01:00',
               ...
               '2019-08-24 14:00:00+02:00', '2019-08-25 14:00:00+02:00',
               '2019-08-26 14:00:00+02:00', '2019-08-26 15:00:00+02:00',
               '2019-08-27 15:00:00+02:00', '2019-08-28 15:00:00+02:00',
               '2019-08-29 14:00:00+02:00', '2019-08-30 14:00:00+02:00',
               '2019-08-31 14:00:00+02:00', '2019-08-31 15:00:00+02:00'],
              dtype='datetime64[ns, Europe/Madrid]', name='Time', length=13493, freq=None)

isinstance(df.index, pd.DatetimeIndex)
True

isinstance(df.index, pd.Int64Index)
True

Note on import program and module versions employed:

I'm using Python 3.7.7 (default, Apr 20 2020, 05:55:00) [GCC 5.4.0 20160609] on linux Lubuntu 18.04 LTS.

As for the pandas version: 1.0.5

Andreas L.
  • 3,239
  • 5
  • 26
  • 65

1 Answers1

1

From the source code:

> class DatetimeIndex(DatetimeTimedeltaMixin, DatetimeDelegateMixin):
>    """
>    Immutable ndarray of datetime64 data, represented internally as int64, and
>    which can be boxed to Timestamp objects that are subclasses of datetime and
>    carry metadata such as frequency information.
> [...]
James
  • 145
  • 5
  • Ok, so pd.DatetimeIndex is a sub-indexclass of pd.Int64Index? – Andreas L. Jul 01 '20 at 09:25
  • 1
    In the `DatetimeIndex.get_loc()` [method](https://github.com/pandas-dev/pandas/blob/v1.0.5/pandas/core/indexes/datetimes.py#L679-L735), the index processes a date object to an `int64` to access the row. – James Jul 01 '20 at 09:48
  • Alright, I've had enough proofs, thanks. Indeed, `pd.DatetimeIndex` is a sub-class of `pd.Int64Index`. It had given me wrong results when filtering index-types and I just noticed it today. – Andreas L. Jul 01 '20 at 09:59