Skip to content

Slicing with reversed datetime index #14316

Closed
@boncelet

Description

@boncelet

I'm working with a time series data file from EIA.gov. It's a CSV file with the dates in reverse order (most recent first). Slicing on the dates wasn't working right. I believe these snippets illustrate the problem:

Create dataframe and date reversed dataframe.

>>> import pandas as pd
>>> dates = pd.date_range('1/1/2016',periods = 5)
>>> df = pd.DataFrame([1,2,3,4,5], index=dates, columns=['Col'])
>>> rdf = df.sort_index(axis=0, ascending=False)

First the normal order: Slice on dates, getting what we expect:

>>> print(df['2016-01-01':'2016-01-03'])
            Col
2016-01-01    1
2016-01-02    2
2016-01-03    3

Now the reverse order: I expect to get exactly the same answer (same slice on the same dataset), perhaps with rows reversed, but don't:

>>> print(rdf['2016-01-01':'2016-01-03'])
Empty DataFrame
Columns: [Col]
Index: []

Perhaps if I reverse the slice. Note only two rows are returned:

>>> print(rdf['2016-01-03':'2016-01-01'])
            Col
2016-01-03    3
2016-01-02    2

This makes sense, but it's still confusing:

>>> print(df[1:3]), print(rdf[1:3])
            Col
2016-01-02    2
2016-01-03    3
            Col
2016-01-04    4
2016-01-03    3

I could sort (or reverse) my index, but I had no reason to believe I needed to. Furthermore, other operations, such as plotting, work as expected (the time axis is correct).

Output of pd.show_versions()

## INSTALLED VERSIONS

commit: None
python: 3.5.2.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 23 Stepping 6, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None

pandas: 0.18.1
nose: 1.3.7
pip: 8.1.2
setuptools: 23.0.0
Cython: 0.24
numpy: 1.11.1
scipy: 0.17.1
statsmodels: 0.6.1
xarray: None
IPython: 4.2.0
sphinx: 1.3.1
patsy: 0.4.1
dateutil: 2.5.3
pytz: 2016.4
blosc: None
bottleneck: 1.1.0
tables: 3.2.2
numexpr: 2.6.0
matplotlib: 1.5.1
openpyxl: 2.3.2
xlrd: 1.0.0
xlwt: 1.1.2
xlsxwriter: 0.9.2
lxml: 3.6.0
bs4: 4.4.1
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.13
pymysql: None
psycopg2: None
jinja2: 2.8
boto: 2.40.0
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions