-
-
Notifications
You must be signed in to change notification settings - Fork 141
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
base: main
Are you sure you want to change the base?
Conversation
DataFrame.pct_change
and Series.pct_change
DataFrame.pct_change
and Series.pct_change
There was a problem hiding this 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, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
:
pandas-stubs/pandas-stubs/core/frame.pyi
Lines 312 to 314 in a6faaf3
def itertuples( | |
self, index: _bool = ..., name: _str = ... | |
) -> Iterable[_PandasNamedTuple]: ... |
There was a problem hiding this comment.
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 = ..., |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 = ..., |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fill_value: object | None = ..., | |
fill_value: Scalar | NAType | None = ..., |
object
is too wide.
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 = ..., |
There was a problem hiding this comment.
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()
.
This PR does the following for the two functions in the title:
replace kwargs with keyword-only parameters for
pct_change
based on parameters fromshift
replace
...
w/ simple defaults when applicable based on docs: https://pandas.pydata.org/docs/reference/api/pandas.Series.pct_change.htmlmake
fill_method
forSeries.pct_change
beNone
, just likeDataFrame.pct_change
since all other values are deprecatedCloses type
kwargs
inpct_change
according to params inshift
#1169Tests added: Please use
assert_type()
to assert the type of any return valuecc @MarcoGorelli should these have tests? they're not overloaded so I'm not sure if it's necessary