Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: DataFrame.truncate() returns incorrect data if the index is sorted in descending order #33756

Closed
2 of 3 tasks
francislan opened this issue Apr 24, 2020 · 0 comments · Fixed by #33769
Closed
2 of 3 tasks
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@francislan
Copy link

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas (1.0.3)

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

df = pd.DataFrame(index=range(0, 5), data={'A': 1})
df.sort_index(ascending=False, inplace=True)
df.truncate(before=3)

Problem description

When the index is sorted in descending order, the truncate() function behaves incorrectly. In the example above, it should truncate all rows whose index is < 3. Instead it is truncating all rows whose index is > 3.
Similarly when using after.

Output

>>> df
   A
4  1
3  1
2  1
1  1
0  1
>>> df.truncate(before=3)
   A
3  1
2  1
1  1
0  1

Expected Output

>>> df.truncate(before=3)
   A
4  1
3  1

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.7.final.0
python-bits : 64
OS : Darwin
OS-release : 19.4.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.0.3
numpy : 1.18.2
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 41.2.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.2.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

@francislan francislan added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 24, 2020
@dsaxton dsaxton added Indexing Related to indexing on series/frames, not to indexes themselves and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 24, 2020
@jreback jreback added this to the 1.1 milestone Apr 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
3 participants