Thomas Philips
Thomas Philips

Reputation: 1089

Pandas to_datetime seems incompatible with numpy datetime objects

Printing the start of the dataframe gives me

print(IRdaily.head())
None
           1 mo  3 mo  6 mo  1 yr  2 yr  3 yr  5 yr  7 yr 10 yr 20 yr 30 yr
Date                                                                       
1990-01-02  NaN  7.83  7.89  7.81  7.87  7.90  7.87  7.98  7.94   NaN  8.00
1990-01-03  NaN  7.89  7.94  7.85  7.94  7.96  7.92  8.04  7.99   NaN  8.04
1990-01-04  NaN  7.84  7.90  7.82  7.92  7.93  7.91  8.02  7.98   NaN  8.04
1990-01-05  NaN  7.79  7.85  7.79  7.90  7.94  7.92  8.03  7.99   NaN  8.06
1990-01-08  NaN  7.79  7.88  7.81  7.90  7.95  7.92  8.05  8.02   NaN  8.09

I'd now like to filter the data by discarding data before a given date, so I tried the following:

IRdaily = IRdaily[IRdaily['Date'] >= datetime.datetime(2000,1,1)]

but I get a KeyError:

IRdaily[IRdaily['Date'] >= dt]
KeyError: 'Date'

also,

type(datetime.datetime(2000,1,1))
<class 'datetime.datetime'>

which doesn't match the dtype of IRdaily['Date']. What am I doing wrong?

Many thanks in advance for your guidance

Thomas Philips

Upvotes: 4

Views: 308

Answers (2)

BENY
BENY

Reputation: 323316

Convert to datetime format by using to_datetime then you can using .loc filter the df

IRdaily.index=pd.to_datetime(IRdaily.index)
IRdaily.loc['1990-01-04':]

Upvotes: 1

jezrael
jezrael

Reputation: 863166

I think there is DatetimeIndex, need compare by .index:

print (df.index)
DatetimeIndex(['1990-01-02', '1990-01-03', '1990-01-04', '1990-01-05',
               '1990-01-08'],
              dtype='datetime64[ns]', freq=None)

IRdaily = IRdaily[IRdaily.index >= datetime.datetime(2000,1,1)]

Or:

IRdaily = IRdaily[IRdaily.index >= '2000-01-01']

Or use query:

IRdaily = IRdaily.query('Date >= "2000-01-01"')

Upvotes: 1

Related Questions