pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
43.31k stars 17.8k forks source link

BUG: Series.diff does no arg validation unlike df.diff for `periods` #56607

Closed sfc-gh-rdurrani closed 8 months ago

sfc-gh-rdurrani commented 9 months ago

Pandas version checks

Reproducible Example

In [1]: import pandas as pd
df =
In [2]: df = pd.DataFrame()

In [3]: df.diff(0.5)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-03698d8a038c> in ?()
----> 1 df.diff(0.5)

~/.miniconda3/envs/snowpark/lib/python3.8/site-packages/pandas/core/frame.py in ?(self, periods, axis)
   9216                 is_float(periods)
   9217                 # error: "int" has no attribute "is_integer"
   9218                 and periods.is_integer()  # type: ignore[attr-defined]
   9219             ):
-> 9220                 raise ValueError("periods must be an integer")
   9221             periods = int(periods)
   9222
   9223         axis = self._get_axis_number(axis)

ValueError: periods must be an integer

In [4]: series = pd.Series()
<ipython-input-4-2b0f0f5d39e8>:1: FutureWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
  series = pd.Series()

In [5]: series.diff(0.5)
Out[5]: Series([], dtype: float64)

In [6]: df.diff('1')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-ffa50df92031> in ?()
----> 1 df.diff('1')

~/.miniconda3/envs/snowpark/lib/python3.8/site-packages/pandas/core/frame.py in ?(self, periods, axis)
   9216                 is_float(periods)
   9217                 # error: "int" has no attribute "is_integer"
   9218                 and periods.is_integer()  # type: ignore[attr-defined]
   9219             ):
-> 9220                 raise ValueError("periods must be an integer")
   9221             periods = int(periods)
   9222
   9223         axis = self._get_axis_number(axis)

ValueError: periods must be an integer

In [7]: series.diff('1')
Out[7]: Series([], dtype: float64)

In [8]: series.diff('a')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[8], line 1
----> 1 series.diff('a')

File ~/.miniconda3/envs/snowpark/lib/python3.8/site-packages/pandas/core/series.py:2903, in Series.diff(self, periods)
   2818 @doc(
   2819     klass="Series",
   2820     extra_params="",
   (...)
   2866 )
   2867 def diff(self, periods: int = 1) -> Series:
   2868     """
   2869     First discrete difference of element.
   2870
   (...)
   2901     {examples}
   2902     """
-> 2903     result = algorithms.diff(self._values, periods)
   2904     return self._constructor(result, index=self.index).__finalize__(
   2905         self, method="diff"
   2906     )

File ~/.miniconda3/envs/snowpark/lib/python3.8/site-packages/pandas/core/algorithms.py:1699, in diff(arr, n, axis)
   1679 def diff(arr, n: int, axis: int = 0):
   1680     """
   1681     difference of n between self,
   1682     analogous to s-s.shift(n)
   (...)
   1696     shifted
   1697     """
-> 1699     n = int(n)
   1700     na = np.nan
   1701     dtype = arr.dtype

ValueError: invalid literal for int() with base 10: 'a'


### Issue Description

Series.diff doesn't validate the `periods` argument, while `df.diff` does.

### Expected Behavior

I think that Series.diff should also validate and throw the same exceptions as df.diff.

### Installed Versions

<details>

INSTALLED VERSIONS
------------------
commit           : 2e218d10984e9919f0296931d92ea851c6a6faf5
python           : 3.8.18.final.0
python-bits      : 64
OS               : Darwin
OS-release       : 23.2.0
Version          : Darwin Kernel Version 23.2.0: Wed Nov 15 21:55:06 PST 2023; root:xnu-10002.61.3~2/RELEASE_ARM64_T6020
machine          : x86_64
processor        : i386
byteorder        : little
LC_ALL           : None
LANG             : en_US.UTF-8
LOCALE           : en_US.UTF-8

pandas           : 1.5.3
numpy            : 1.24.3
pytz             : 2023.3.post1
dateutil         : 2.8.2
setuptools       : 68.0.0
pip              : 23.2.1
Cython           : None
pytest           : 7.4.3
hypothesis       : None
sphinx           : 5.0.2
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : None
html5lib         : None
pymysql          : None
psycopg2         : None
jinja2           : 3.1.2
IPython          : 8.12.3
pandas_datareader: None
bs4              : 4.12.2
bottleneck       : 1.3.5
brotli           : None
fastparquet      : None
fsspec           : None
gcsfs            : None
matplotlib       : 3.7.3
numba            : None
numexpr          : 2.8.4
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : 10.0.1
pyreadstat       : None
pyxlsb           : None
s3fs             : None
scipy            : None
snappy           : None
sqlalchemy       : None
tables           : None
tabulate         : None
xarray           : None
xlrd             : None
xlwt             : None
zstandard        : None
tzdata           : 2023.3
</details>
lithomas1 commented 8 months ago

Thanks for reporting this - I can reproduce this on the main branch.

Are you interested in making a PR?

pmhatre1 commented 8 months ago

Can I support you @lithomas1 ? I am new to open source and want to contribute.

lithomas1 commented 8 months ago

Sure, contributing instructions can be found here https://pandas.pydata.org/docs/dev/development/contributing.html.

After you get set up, you'll want to figure out how dataframe validates the periods (the traceback should help you here) and port it over to the series method(which can be found in pandas/core/series.py)

Then, you'll want to add a test in pandas/tests/series/methods/test_diff.py(you can grep for the error message "periods must be an integer" to see how the test is done for the dataframe method), and a note to the whatsnew in doc/source/v2.3.0.rst.

srinivaspavan9 commented 8 months ago

Hi I started working on this issue, i have found that there is no validation check in series, and series is using algorithms.diff(periods)

neha3004 commented 8 months ago

I think I fixed this issue.

Here is the pull request: https://github.com/pandas-dev/pandas/pull/56659

Please let me know in case of any issues.

srinivaspavan9 commented 8 months ago

Hi I have raised a PR addressing this issues,

Ready for Review

pmhatre1 commented 8 months ago

Thanks a lot, @lithomas1 for sharing the document. I have submitted the PR request for this issue. Not sure, how this is addressed with three people contributing.

lithomas1 commented 8 months ago

@neha3004 @srinivaspavan9

I think @pmhatre1 started working on this before you, so I've closed your PRs.

I'm happy to help you get started with some other issues if you find any other interesting issues on the issue tracker, though.