Exi
Exi

Reputation: 320

Python: Indexing in Pandas with Datetime indices

I've got problems regarding to indexing in Pandas and hope you can help me:

rng = pd.date_range('2015-12-31 21:00:00', periods=7, freq='H')
df = pd.DataFrame({ 'Val' : np.random.randn(len(rng)) }, index=rng) 
first_value_of_year = df['2016'].first('1H').index

returns the first value of the year as DatetimeIndex:

DatetimeIndex(['2016-01-01'], dtype='datetime64[ns]', freq='H')

When I call the dataframe with this index, everything seems to be working fine:

df.loc[first_value_of_year]

returns

+------------------------+-----------+
|                        |  Val      |
+------------------------+-----------+
| 2016-01-01             | 0.144044  |

So, everything is OK up to here! But if I want to get all values greater than this value, it doesn't work anymore:

df.loc[df.index >= first_value_of_year]

and gives ValueError (lenghts must match):

but if I take the same command with the timestamp itself as string it does what it should do

df.loc[df.index >= '2016-01-01 00:00:00']

returns

+------------------------+-----------+
|                        |  Val      |
+------------------------+-----------+
| 2016-01-01 01:00:00    | 1.454274  |
| 2016-01-01 02:00:00    | 0.761038  |
| 2016-01-01 03:00:00    | 0.121675  |

so, what am I missing here? How do I correctly access all values greater than the DatetimeIndex variable?

Thanks!

Upvotes: 1

Views: 148

Answers (1)

jezrael
jezrael

Reputation: 863481

I believe you need select first value of index to scalar by indexing - [0]:

df.loc[df.index >= first_value_of_year[0]]

Upvotes: 3

Related Questions