I'd like to be able to figure out if I can get the following to work (Pandas 0.23.4). Any help would be most appreciated.
import numpy as np
import pandas as pd
rows = 12
rng = pd.date_range('2011-01', periods=rows, freq='M')
df = pd.DataFrame(np.arange(rows), index=rng)
print(df.loc['2011-01'])
print(df.loc[np.datetime64('2011-01')])
The first print does what I would expect: shows all the rows that are in Jan of 2011. However, the second one throws an KeyError because the value is not in the index. I was hoping that it would provide the same output, but after some testing I realize that it is looking for an exact match 2011-01-01, which is not in the DataFrame. I'd like for the second one to work, so that I can use numpy.arange or pandas.date_range to easily generate arrays of dates that I can loop through. Anyone got this to work?
(Seems like this works, but only if you have an exact match for the dates.)