Pandas DataFrame.loc returns Empty DataFrame

Viewed 5792

Currently trying to work through some tutorials. Right now I have a pandas Dataframe with stock data. The stock data was taken by reading a csv file, then using df.set_index('timestamp', inplace = True) I set the index to use 'timestamp'. The head is seen 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 row 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 returned as so:

Empty DataFrame
Columns: [open, high, low, close, adjusted_close, volume, dividend_amount, 
split_coefficient]
Index: []

Was wondering if anyone could provide any insight as to why this is happening? I would have expected a return of the information between the two dates instead of the empty dataframe. Thanks!

5 Answers

sort_index() before slicing would work:

df = df.sort_index()
df['2018-09-05':'2018-09-11']

or

df = df.sort_index()
df.loc['2018-09-05':'2018-09-11']

FYI: If you want to make sure that index is DatetimeIndex before making 'timestamp' as index:

df.timestamp = pd.to_datetime(df.timestamp)
df = df.set_index('timestamp')

or after the fact:

df.index = pd.to_datetime(df.index)

Because you need to insert your index correctly from up to bottom. Try this for specific indexing:

df.loc['2018-09-11':'2018-09-05']

But you can use sort() to sort your index.
You can also use the ID for this.

If the timestamp column is Pandas Timestamp, You can do something like this.

mask = (df['timestamp'] >= pd.Timestamp('2018-09-05')) & (df['timestamp'] <= pd.Timestamp('2018-09-11'))

df.loc[mask, :]

You just needed to reverse dates from newer to older and it will work.

import pandas as pd
df = pd.read_csv('some.csv', delimiter=";", index_col='timestamp')
range = df.loc['2018-09-11':'2018-09-05']
print(range)

It works if you change the date format

df.loc['05/09/2018': '11/09/2018']
Related