Skip to content

BUG: truncate has wrong behavior when the Index has only one unique value #42365

Closed
@neelmraman

Description

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

  • I have confirmed this bug exists on the latest version of pandas.

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


Code Sample, a copy-pastable example

# Returns an empty Series/DataFrame when the one you pass in has length > 1 and an index with only one unique value
x = pd.Series(0, index=pd.date_range('2021-06-29', '2021-06-29')).repeat(5)
x.truncate('2021-06-28', '2021-07-01')

# If you add another unique index value, you get the expected behavior
x = pd.Series(0, index=pd.date_range('2021-06-29', '2021-06-29')).repeat(5)
y = pd.Series(0, index=pd.date_range('2021-06-01', '2021-06-01'))
x = x.append(y)
x.truncate('2021-06-28', '2021-07-01')

Problem description

I would expect truncate to not do any filtering in the first case given that all index values are between before and after. The fix is trivial, so I'll submit a PR.

Expected Output

2021-06-29    0
2021-06-29    0
2021-06-29    0
2021-06-29    0
2021-06-29    0

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 7c48ff4
python : 3.9.5.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19043
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 1.2.5
numpy : 1.20.2
pytz : 2021.1
dateutil : 2.8.1
pip : 21.1.3
setuptools : 52.0.0.post20210125
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.1
IPython : 7.22.0
pandas_datareader: None
bs4 : None
bottleneck : 1.3.2
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : 2.7.3
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

Metadata

Metadata

Assignees

Labels

BugIndexingRelated to indexing on series/frames, not to indexes themselves

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions