Skip to content

BUG: float period not supported in df.diff() since 1.1.5 #41229

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

Closed
3 tasks done
auderson opened this issue Apr 30, 2021 · 8 comments
Closed
3 tasks done

BUG: float period not supported in df.diff() since 1.1.5 #41229

auderson opened this issue Apr 30, 2021 · 8 comments
Labels
Numeric Operations Arithmetic, Comparison, and Logical operations Usage Question

Comments

@auderson
Copy link
Contributor

auderson commented Apr 30, 2021

  • 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.


previous commit #36710

def diff(self, periods: int = 1, axis: Axis = 0) -> DataFrame:
    if not isinstance(periods, int):
        if not (is_float(periods) and periods.is_integer()):
            raise ValueError("periods must be an integer")
        periods = int(periods)

    bm_axis = self._get_block_manager_axis(axis)

    if bm_axis == 0 and periods != 0:
        return self - self.shift(periods, axis=axis)

    new_data = self._mgr.diff(n=periods, axis=bm_axis)
    return self._constructor(new_data).__finalize__(self, "diff")

If periods is float, then it can't pass (is_float(periods) and periods.is_integer()), and ValueError will always be raised
So I assume it should be or here?

# suggested:
# here str can't pass through, I'm not sure if str should be supported as well
if not (is_float(periods) or is_integer(periods)):
    raise ValueError("periods must be an integer or float")

Besides, I think .diff() should support float for backward compatibility

Output of pd.show_versions()

INSTALLED VERSIONS

commit : f2c8480
python : 3.8.8.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-140-generic
Version : #144-Ubuntu SMP Fri Mar 19 14:12:35 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.2.3
numpy : 1.20.2
pytz : 2021.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 49.6.0.post20210108
Cython : 0.29.22
pytest : 6.2.3
hypothesis : None
sphinx : 3.5.3
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.2
html5lib : 1.1
pymysql : 0.9.3
psycopg2 : 2.8.6 (dt dec pq3 ext lo64)
jinja2 : 2.11.3
IPython : 7.22.0
pandas_datareader: 0.9.0
bs4 : 4.9.3
bottleneck : None
fsspec : 0.8.5
fastparquet : None
gcsfs : None
matplotlib : 3.3.3
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.5
pandas_gbq : None
pyarrow : 3.0.0
pyxlsb : None
s3fs : None
scipy : 1.6.2
sqlalchemy : None
tables : 3.6.1
tabulate : None
xarray : None
xlrd : 2.0.1
xlwt : None
numba : 0.53.1563e
python : 3.8.8.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-140-generic
Version : #144-Ubuntu SMP Fri Mar 19 14:12:35 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.2.3
numpy : 1.20.2
pytz : 2021.1
dateutil : 2.8.1
pip : 21.0.1
setuptools : 49.6.0.post20210108
Cython : 0.29.22
pytest : 6.2.3
hypothesis : None
sphinx : 3.5.3
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.2
html5lib : 1.1
pymysql : 0.9.3
psycopg2 : 2.8.6 (dt dec pq3 ext lo64)
jinja2 : 2.11.3
IPython : 7.22.0
pandas_datareader: 0.9.0
bs4 : 4.9.3
bottleneck : None
fsspec : 0.8.5
fastparquet : None
gcsfs : None
matplotlib : 3.3.3
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.5
pandas_gbq : None
pyarrow : 3.0.0
pyxlsb : None
s3fs : None
scipy : 1.6.2
sqlalchemy : None
tables : 3.6.1
tabulate : None
xarray : None
xlrd : 2.0.1
xlwt : None
numba : 0.53.1

@auderson auderson added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 30, 2021
@auderson auderson changed the title BUG: float period not supported in df.diff() BUG: float period not supported in df.diff() since 1.1.5 Apr 30, 2021
@auderson
Copy link
Contributor Author

or just

try:
    periods = int(periods)
except Exception as e:
    raise ValueError(f"periods '{periods}' can't be converted as int") from e

@jbrockmendel
Copy link
Member

(is_float(periods) and periods.is_integer())

is_float is equivalent to isinstance(periods, (float, np.floating)). is_integer is a float method checking x == int(x)

@auderson
Copy link
Contributor Author

@jbrockmendel
so float number like 3.21 is no longer supported?
In earlier versions they will be convert to int 3, now will cause ValueError

@jbrockmendel
Copy link
Member

correct

@auderson
Copy link
Contributor Author

@jbrockmendel
Got it, thanks for reply!

@rhshadrach
Copy link
Member

+1 on raising for non-integral values like 3.21, @auderson you can pass int(3.21) if this fits your use case.

@rhshadrach rhshadrach added Usage Question Numeric Operations Arithmetic, Comparison, and Logical operations and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 30, 2021
@rhshadrach
Copy link
Member

@auderson - closing this issue. If there is anything else, feel free to reply here and can reopen.

@auderson
Copy link
Contributor Author

@rhshadrach
Yes, I'm working on that. I have some algos generated from Genetic Programming that will have random float as periods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Numeric Operations Arithmetic, Comparison, and Logical operations Usage Question
Projects
None yet
Development

No branches or pull requests

3 participants