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

PERF: shift() of boolean series gives drawdown by an order of magnitude with default filling np.NaN comparing with filling by bool like False #58465

Closed
3 tasks done
TsiVit opened this issue Apr 28, 2024 · 7 comments · Fixed by #58588
Labels

Comments

@TsiVit
Copy link

TsiVit commented Apr 28, 2024

Pandas version checks

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

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

  • I have confirmed this issue exists on the main branch of pandas.

Reproducible Example

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(10**6))<0
%timeit -n5 -r20 df.shift(1)

41.3 ms ± 6.21 ms per loop (mean ± std. dev. of 20 runs, 5 loops each)

Installed Versions

INSTALLED VERSIONS

commit : d9cdd2e
python : 3.11.4.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 9, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1251

pandas : 2.2.2
numpy : 1.25.1
pytz : 2023.3
dateutil : 2.8.2
setuptools : 65.5.0
pip : 24.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.14.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.2
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2023.10.0
gcsfs : None
matplotlib : 3.7.2
numba : 0.58.1
numexpr : None
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : 13.0.0
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

Prior Performance

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(10**6))<0
%timeit -n5 -r20 df.shift(1, fill_value=False)

389 µs ± 110 µs per loop (mean ± std. dev. of 20 runs, 5 loops each)

@TsiVit TsiVit added Needs Triage Issue that has not been reviewed by a pandas team member Performance Memory or execution speed performance labels Apr 28, 2024
@rhshadrach
Copy link
Member

I think this is expected since df.shift(1) needs to coerce the dtype to object in order to hold True, False, and np.nan.

df = pd.DataFrame(np.random.randn(10**6)) < 0
print(df.shift(1).dtypes)
# 0    object
# dtype: object
print(df.shift(1, fill_value=False).dtypes)
# 0    bool
# dtype: object

@rhshadrach rhshadrach added Closing Candidate May be closeable, needs more eyeballs Transformations e.g. cumsum, diff, rank and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 29, 2024
@TsiVit
Copy link
Author

TsiVit commented Apr 30, 2024

I have expected boolean filling for boolean series by default as according the docs:

the default depends on the dtype of self. For numeric data, np.nan is used. For datetime, timedelta, or period data, etc. NaT is used. For extension dtypes, self.dtype.na_value is used.

@rhshadrach
Copy link
Member

rhshadrach commented Apr 30, 2024

Do you see something that disagrees with the docs? If that is the case, please provide an example, the result you get, and the result you expect.

@rhshadrach rhshadrach added the Needs Info Clarification about behavior needed to assess issue label Apr 30, 2024
@TsiVit
Copy link
Author

TsiVit commented May 1, 2024

Yes, according the docs if I have a boolean series, instead of NaN filling of missed values have to be boolean by False (as False is default boolean state)

the default depends on the dtype of self.

@TsiVit
Copy link
Author

TsiVit commented May 1, 2024

but anyway, if no one met that before - it's not a problem and only my interpretation, and fill_value make a deal for somebody like me in such case.

@rhshadrach
Copy link
Member

rhshadrach commented May 1, 2024

Yes, according the docs if I have a boolean series, instead of NaN filling of missed values have to be boolean by False

I do not believe the docs state this. Saying the default depends on the dtype of self does not mean that False is used for Boolean data, and in fact I do not think that should be the behavior. The default value should represent "missing".

In any case, I do think it would improve the docs to read

For Boolean and numeric NumPy data types, np.nan is used.

A PR would be welcome to improve the documentation!

@rhshadrach rhshadrach added Docs good first issue and removed Performance Memory or execution speed performance Needs Info Clarification about behavior needed to assess issue Closing Candidate May be closeable, needs more eyeballs labels May 1, 2024
@SongYeongEng
Copy link
Contributor

Hi @rhshadrach, I tried to create a PR to update the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants