Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: Accessing data with list of dates and DateTimeIndex

I have a pandas DataFrame with a DateTimeIndex:

                           A          B
2016-04-25 18:50:06   440.967796   201.049600  
2016-04-25 18:50:13   441.054995   200.767034  
2016-04-25 18:50:20   441.142337   200.484475
...
2016-07-27 18:50:06   440.967796   201.049600  
2016-07-27 18:50:13   441.054995   200.767034  
2016-07-27 18:50:20   441.142337   200.484475

I would like to extract all the data of a given date yyyy-mm-dd using a list of dates: ['2016-04-25','2016-04-28',...]

I tried the following:

 df[df.index.isin(['2016-04-25', '2016-04-26'])]

 Empty DataFrame

I would like to retrieve all the data (data of the whole day) of the dates given in this list

like image 931
user3142067 Avatar asked Aug 29 '17 17:08

user3142067


People also ask

How can I get DatetimeIndex in pandas?

date attribute outputs an Index object containing the date values present in each of the entries of the DatetimeIndex object. Example #1: Use DatetimeIndex. date attribute to find the date part of the DatetimeIndex object.

How do I work with dates and times in pandas?

Pandas has a built-in function called to_datetime()that converts date and time in string format to a DateTime object. As you can see, the 'date' column in the DataFrame is currently of a string-type object. Thus, to_datetime() converts the column to a series of the appropriate datetime64 dtype.

How do I find a data frame between two dates?

In order to select rows between two dates in pandas DataFrame, first, create a boolean mask using mask = (df['InsertedDates'] > start_date) & (df['InsertedDates'] <= end_date) to represent the start and end of the date range. Then you select the DataFrame that lies within the range using the DataFrame.


1 Answers

You need remove times first by this solutions:

df = df[df.index.normalize().isin(['2016-04-25', '2016-04-26'])]

df = df[df.index.floor('D').isin(['2016-04-25', '2016-04-26'])]

Another solution is compare DatetimeIndex.date, but necessary use numpy.in1d instead isin:

df = df[np.in1d(df.index.date, pd.to_datetime(['2016-04-25', '2016-04-26']).date)]

Or compare strings created DatetimeIndex.strftime:

df = df[np.in1d(df.index.strftime('%Y-%m-%d'), ['2016-04-25', '2016-04-26'])]

print (df)
                              A           B
2016-04-25 18:50:06  440.967796  201.049600
2016-04-25 18:50:13  441.054995  200.767034
2016-04-25 18:50:20  441.142337  200.484475
like image 107
jezrael Avatar answered Sep 18 '22 14:09

jezrael