Skip to content

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

Closed
@auderson

Description

@auderson
  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions