1

I have a df with two columns name and time. I want to find the rows between the inputs - input1 and input2

input1 = pd.Period('4/21/2020') 
input2 = pd.Period('5/20/2020')

df

    raw_data = {
        'name': ['a', 'b', 'c', 'd','e','f','g','h','i'], 
        'time': ['2020-03-12','2020-03-16','2020-04-29','2020-03-16',
                 '2020-03-16','2020-04-16','2020-03-09','2020-05-19','2020-04-23']
   } 

    df = pd.DataFrame(raw_data, columns = ['name', 'time'])


           name        time
    0       a         2020-03-12
    1       b         2020-03-16
    2       c         2020-04-29
    3       d         2020-03-16
    4       e         2020-03-16
    5       f         2020-04-16
    6       g         2020-03-09
    7       h         2020-05-19
    8       i         2020-04-23

My expected output:

df

           name        time
    2       c         2020-04-29
    7       h         2020-05-19
    8       i         2020-04-23

What i have tried:

    df['time'] = pd.to_datetime(df['time'])
    df = df[df['time'].dt.to_period('d').isin([input1, input2])]
yoskovia
  • 350
  • 2
  • 10
  • Does this answer your question? [Select DataFrame rows between two dates](https://stackoverflow.com/questions/29370057/select-dataframe-rows-between-two-dates) – Ben.T May 21 '20 at 16:40
  • I'm a bit unsure why you use Period here, but it would work the same way than with dates like in the link above – Ben.T May 21 '20 at 16:41

1 Answers1

1

Your input values are Periods which need to be converted to timestamp,

df.loc[(df['time'] > input1.to_timestamp()) & (df['time'] < input2.to_timestamp())]



   name time
2   c   2020-04-29
7   h   2020-05-19
8   i   2020-04-23
Vaishali
  • 37,545
  • 5
  • 58
  • 86