Skip to content

GH1169 Improve parameter types for DataFrame.pct_change and Series.pct_change #1194

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

yangdanny97
Copy link
Contributor

This PR does the following for the two functions in the title:

cc @MarcoGorelli should these have tests? they're not overloaded so I'm not sure if it's necessary

@yangdanny97 yangdanny97 changed the title Improve parameter types for DataFrame.pct_change and Series.pct_change GH1169 Improve parameter types for DataFrame.pct_change and Series.pct_change Apr 22, 2025
Copy link
Collaborator

@Dr-Irv Dr-Irv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add tests for the various arguments in test_frame.py:test_dataframe_pct_change() ?

@@ -1987,11 +1987,13 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
def ne(self, other, axis: Axis = ..., level: Level | None = ...) -> Self: ...
def pct_change(
self,
periods: int = ...,
periods: int = 1,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't do this in a stubs. Please leave it as periods: int = ... because the periods argument is optional.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, could you please clarify? The argument is still optional when written like this.

I see a few other places where the default is written when it's a simple literal, like https://github.com/pandas-dev/pandas-stubs/blob/main/pandas-stubs/core/frame.pyi#L317

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the types ... means you just take whatever the value is in the definition of the method (in the pandas code so that allows to keep track of the default value in only one place).
The itertools you are pointing to is an overload which is different than the function you are modifying here. The overload adapts the return type to a certain type of input types you are passing. For the example you provide, it is hard-coded since you are forcing the value to be None and that does not give the user for anything else, whereas you see that the methods also allows for the type str:

def itertuples(
self, index: _bool = ..., name: _str = ...
) -> Iterable[_PandasNamedTuple]: ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood, thank you

@@ -1987,11 +1987,13 @@ class DataFrame(NDFrame, OpsMixin, _GetItemHack):
def ne(self, other, axis: Axis = ..., level: Level | None = ...) -> Self: ...
def pct_change(
self,
periods: int = ...,
periods: int = 1,
fill_method: None = ...,
limit: int | None = ...,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

limit has been deprecated, so remove this

fill_method: None = ...,
limit: int | None = ...,
freq=...,
**kwargs: Any, # TODO: make more precise https://github.com/pandas-dev/pandas-stubs/issues/1169
freq: Frequency | dt.timedelta | None = None,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
freq: Frequency | dt.timedelta | None = None,
freq: DateOffset| dt.timedelta | None = None,

Frequency is too wide. Should be changed in shift too. That's old code.

freq: Frequency | dt.timedelta | None = None,
*,
axis: AxisIndex = ...,
fill_value: object | None = ...,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fill_value: object | None = ...,
fill_value: Scalar | NAType | None = ...,

object is too wide.

Comment on lines +1551 to +1557
periods: int = 1,
fill_method: None = ...,
limit: int | None = ...,
freq=...,
**kwargs: Any, # TODO: make more precise https://github.com/pandas-dev/pandas-stubs/issues/1169
freq: Frequency | timedelta | None = None,
*,
axis: AxisIndex = ...,
fill_value: object | None = ...,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above on DataFrame.pct_change() . Also, remove axis as it is not used in Series.pct_change().

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

Successfully merging this pull request may close these issues.

type kwargs in pct_change according to params in shift
3 participants