Pandas: timeseries KeyError

Created on 22 Jun 2018  路  3Comments  路  Source: pandas-dev/pandas

Code Sample, a copy-pastable example if possible

ts = pd.DataFrame({'date': pd.date_range(start = '1/1/2017', periods = 5),
                   'observations': range(10, 60, 10)}).set_index('date')

print(ts['2017-01-02':'2017-01-04'])

            observations
date                    
2017-01-02            20
2017-01-03            30
2017-01-04            40

print(ts['2017-01-03'])

Generates KeyError: '2017-01-03'

print(ts['2017-01-03':'2017-01-03'])

            observations
date                    
2017-01-03            30

print(ts.loc['2017-01-03'])

observations    30
Name: 2017-01-03 00:00:00, dtype: int64

Problem description

When using timeseries I expected to be able to select one single date row using ts['2017-01-03'], but that generates a KeyError.

To select one single date row I had to use ts['2017-01-03':'2017-01-03'] or ts.loc['2017-01-03']. But using a date range to select one date row or 'switching' to the .loc syntax feel a bit uncomfortable.

I am aware that syntax df['column_name'] is a 'direct column reference' and not a as in df[<logical condition>]. Bit in my option it would be very nice if ts['2017-01-03'] is evaluated as a logical condition, not as a direct column reference when a DatetimeIndex is used.

Expected Output

ts['2017-01-03']

            observations
date                    
2017-01-03            30

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.5.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.23.1
pytest: 3.6.1
pip: 10.0.1
setuptools: 39.2.0
Cython: 0.28.3
numpy: 1.14.5
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 6.4.0
sphinx: 1.7.5
patsy: 0.5.0
dateutil: 2.7.3
pytz: 2018.4
blosc: None
bottleneck: 1.2.1
tables: 3.4.4
numexpr: 2.6.5
feather: None
matplotlib: 2.2.2
openpyxl: 2.5.4
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.5
lxml: 4.2.1
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.2.8
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Indexing Timeseries Usage Question

All 3 comments

Error can be reproduced at master

@Brinkhuis : That indeed seems odd! Investigation and PR are welcome!

see the docs: http://pandas.pydata.org/pandas-docs/stable/timeseries.html#slice-vs-exact-match

this simply leads to ambiguity and was disallowed several releases go.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ericdf picture ericdf  路  3Comments

Ashutosh-Srivastav picture Ashutosh-Srivastav  路  3Comments

matthiasroder picture matthiasroder  路  3Comments

tade0726 picture tade0726  路  3Comments

marcelnem picture marcelnem  路  3Comments