Pandas slice failed due to a list of DatetimeIndex tags… here is a solution to the problem.
Pandas slice failed due to a list of DatetimeIndex tags
I have a 12×3 data frame for storing the monthly average, maximum, and minimum temperature for 2017:
import pandas as pd
tavg = pd. Series([55.8, 57.2, 61.0, 63.6, 63.3, 66.7, 71.7, 72.0, 71.5, 71.5, 65.6, 61.4])
tmax = pd. Series([62.7, 62.6, 69.3, 71.9, 69.4, 72.6, 77.5, 77.3, 78.5, 80.4, 73.7, 72.4])
tmin = pd. Series([48.8, 51.8, 52.8, 55.4, 57.1, 60.9, 65.8, 66.8, 64.6, 62.5, 57.3, 50.3])
temp_df = pd. DataFrame()
temp_df['TAVG'] = tavg
temp_df['TMIN'] = tmin
temp_df['TMAX'] = tmax
dates = pd.date_range(start='2017-01-01', end='2017-12-01', freq='MS')
temp_df.index = dates
I want to slice the list of dates by tags, such as 2017-01-01 and 2017-05-01.
Slice each of these dates individually and work fine:
temp_df.loc['2017-01-01']
TAVG 55.8
TMIN 48.8
TMAX 62.7
Name: 2017-01-01 00:00:00, dtype: float64
temp_df.loc['2017-05-01']
TAVG 63.3
TMIN 57.1
TMAX 69.4
Name: 2017-05-01 00:00:00, dtype: float64
However, slicing with a list of these dates throws KeyError:
temp_df.loc[['2017-01-01', '2017-05-01']]
"KeyError: "None of [['2017-01-01', '2017-05-01']] are in the [index]"
Are there any special tricks for slicing with indexed tag lists when using DatetimeIndex?
Solution
Slice failed because the index is of type Timestamp
. Code temp_df.loc[[‘2017-01-01’, '2017-05-01']]
looks for string labels. Use the following code to parse them as Timestamps
.
temp_df.loc[[pd. Timestamp('2017-01-01'), pd. Timestamp('2017-05-01')]]
Output:
TAVG TMIN TMAX
2017-01-01 55.8 48.8 62.7
2017-05-01 63.3 57.1 69.4
In order for your code to work, you need to change the index type to str
temp_df.index = temp_df.index.astype(str)
temp_df.loc[['2017-01-01', '2017-05-01']]
Output:
TAVG TMIN TMAX
2017-01-01 55.8 48.8 62.7
2017-05-01 63.3 57.1 69.4