How can I get a range of underlying indices number from DataFrame with DateTimeIndex? Some rows are removed, so the values may not be sequential.
For example:
dates = pd.date_range('1/1/2000', periods=8)
df = pd.DataFrame(np.random.randn(8, 4), index=dates, columns=['A', 'B', 'C', 'D'])
df
Out[3]:
A B C D
2000-01-01 0.469112 -0.282863 -1.509059 -1.135632
2000-01-02 1.212112 -0.173215 0.119209 -1.044236
2000-01-03 -0.861849 -2.104569 -0.494929 1.071804
2000-01-04 0.721555 -0.706771 -1.039575 0.271860
2000-01-05 -0.424972 0.567020 0.276232 -1.087401
2000-01-06 -0.673690 0.113648 -1.478427 0.524988
2000-01-07 0.404705 0.577046 -1.715002 -1.039268
2000-01-08 -0.370647 -1.157892 -1.344312 0.844885
For df.index I would like instead of
DatetimeIndex(['2001-01-01', '2001-01-02', '2001-01-03' ...], dtype='datetime64[ns]', freq=None)
to get
[1, 2, 4, ...,26, etc]
Is this possible in a wat that does not include df.reset_index()?
To get this:
[1, 2, 4, ...,26, etc]
without using df.reset_index() (ie; leaving the DatetimeIndex as is), why don't you iterate on the Range of the Length of the index itself:
range(df.index.shape[0])
and to get a list directly you may use List Comprehension:
[i for i in range(df.index.shape[0])]
You might also want to check df.index.get_loc(), which returns the integer location of a specific requested index label.
unique_index = pd.Index(list('abc'))
unique_index.get_loc('b')
>>> 1
All of this is assuming you will be using this list for something other than direct DataFrame indexing of course. (Because obviously this won't work for that!)
You can get integer index from datetimeindex by
import numpy as np
np.where(df.index.isin(df.index))
Output:
(array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
dtype=int64),)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With