1

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!

1
  • I could not understand if it works the way you want or not? Also, printing the type of variables inside the square brackets may give you a hint, e.g. print(type(df.index >= first_value_of_year)) Commented Aug 29, 2019 at 10:34

1 Answer 1

3

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

df.loc[df.index >= first_value_of_year[0]] 
Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much... have spent over 2 hours on this :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.