Shivam Gaur Shivam Gaur - 3 days ago 6
Python Question

Finding if atleast one entry from a specified month string exists in Pandas datetime index

I have a pandas dataframe

test_df
that looks like this:

reading
01-Jan-2016 00:00:00.000 20.464020
02-Jan-2016 00:00:00.000 22.440950
03-Jan-2016 00:00:00.000 27.181500
04-Jan-2016 00:00:00.000 25.318260
05-Jan-2016 00:00:00.000 25.376050
06-Jan-2016 00:00:00.000 0.067112
07-Jan-2016 00:00:00.000 19.313950
08-Jan-2016 00:00:00.000 26.677340
09-Jan-2016 00:00:00.000 26.801620
10-Jan-2016 00:00:00.000 22.583950
11-Jan-2016 00:00:00.000 0.002765
12-Jan-2016 00:00:00.000 26.496440
13-Jan-2016 00:00:00.000 23.233720
14-Jan-2016 00:00:00.000 23.956080
15-Jan-2016 00:00:00.000 26.958120
16-Jan-2016 00:00:00.000 27.351270
17-Jan-2016 00:00:00.000 28.348710
18-Jan-2016 00:00:00.000 25.494090
19-Jan-2016 00:00:00.000 26.342880
20-Jan-2016 00:00:00.000 24.645530


The problem: given a string like '2016-01' a.k.a 'yyyy-mm', I want to find out if
any
entry from the specified month is present in the index of the pandas dataframe
test_df
.

What I'm expecting is
True
for '2016-01' and
False
for any other string. Looking for the most concise method to do that.

Problem setup:



To make things easy, this is the code to obtain test dataframe:

import pandas as pd
temp_df = pd.read_json('{"reading":{"01-Jan-2016 00:00:00.000":20.46402,"02-Jan-2016 00:00:00.000":22.44095,"03-Jan-2016 00:00:00.000":27.1815,"04-Jan-2016 00:00:00.000":25.31826,"05-Jan-2016 00:00:00.000":25.37605,"06-Jan-2016 00:00:00.000":0.06711243,"07-Jan-2016 00:00:00.000":19.31395,"08-Jan-2016 00:00:00.000":26.67734,"09-Jan-2016 00:00:00.000":26.80162,"10-Jan-2016 00:00:00.000":22.58395,"11-Jan-2016 00:00:00.000":0.002765084,"12-Jan-2016 00:00:00.000":26.49644,"13-Jan-2016 00:00:00.000":23.23372,"14-Jan-2016 00:00:00.000":23.95608,"15-Jan-2016 00:00:00.000":26.95812,"16-Jan-2016 00:00:00.000":27.35127,"17-Jan-2016 00:00:00.000":28.34871,"18-Jan-2016 00:00:00.000":25.49409,"19-Jan-2016 00:00:00.000":26.34288,"20-Jan-2016 00:00:00.000":24.64553}}')


I've tried:

>>'2016-01' in test_df.index
False

Answer

If use DatetimeIndex you can use to_period for convert to PeriodIndex and then any (thank you John Zwinck ):

s = '2016-01'

print (temp_df.index.to_period('m'))
PeriodIndex(['2016-01', '2016-01', '2016-01', '2016-01', '2016-01', '2016-01',
             '2016-01', '2016-01', '2016-01', '2016-01', '2016-01', '2016-01',
             '2016-01', '2016-01', '2016-01', '2016-01', '2016-01', '2016-01',
             '2016-01', '2016-01'],
            dtype='period[M]', freq='M')

print (temp_df.index.to_period('m') == '2016-01')
[ True  True  True  True  True  True  True  True  True  True  True  True
  True  True  True  True  True  True  True  True]

print ((temp_df.index.to_period('m') == '2016-01').any())
True
Comments