Pandas DataFrame.loc returns an empty DataFrame
Some tutorials are currently being tried. Now I have a pandas Dataframe with stock data. The stock data was fetched by reading the csv file and then using df.set_index(‘timestamp’, inplace = true)
I set the index to use ‘timestamp’. Head to see here:
timestamp open high low close adjusted_close volume dividend_amount split_coefficient
2018-09-11 74.95 75.69 74.76 75.64 75.64 2225700 0.0 1.0
2018-09-10 75.10 75.21 74.84 74.91 74.91 1774400 0.0 1.0
2018-09-07 75.20 75.20 74.72 75.01 75.01 1804800 0.0 1.0
2018-09-06 74.95 75.55 74.62 75.24 75.24 3058300 0.0 1.0
2018-09-05 75.03 75.42 74.80 74.95 74.95 2418400 0.0 1.0
When I try df.loc['2018-09-05']
, it returns the correct line to me. However, when I try to select a range, such as df.loc[‘2018-09-05’:'2018-09-11']
, I get an empty dataframe that returns:
Empty DataFrame
Columns: [open, high, low, close, adjusted_close, volume, dividend_amount,
split_coefficient]
Index: []
Wondering if anyone can provide any insight into why this is happening? I would have liked to return information between two dates instead of an empty data frame. Thanks!
Solution
sort_index()
works before slicing:
df = df.sort_index()
df['2018-09-05':'2018-09-11']
or
df = df.sort_index()
df.loc['2018-09-05':'2018-09-11']
Citation only: If you want to make sure the index is DatetimeIndex
: before indexing ‘timestamp'
df.timestamp = pd.to_datetime(df.timestamp)
df = df.set_index('timestamp')
Or afterthought:
df.index = pd.to_datetime(df.index)