From 55a40f52973097dab3d96271eb2b6323f2a975ce Mon Sep 17 00:00:00 2001 From: David Mrva Date: Sat, 18 Apr 2020 17:10:00 +0100 Subject: [PATCH 01/20] replaced Appender with doc --- pandas/core/generic.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 9ed9db801d0a8..ad04e5d9f08c9 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -10727,17 +10727,17 @@ def _doc_parms(cls): """ _num_ddof_doc = """ -%(desc)s +{desc} Parameters ---------- -axis : %(axis_descr)s +axis : {axis_descr} skipna : bool, default True Exclude NA/null values. If an entire row/column is NA, the result will be NA. level : int or level name, default None If the axis is a MultiIndex (hierarchical), count along a - particular level, collapsing into a %(name1)s. + particular level, collapsing into a {name1}. ddof : int, default 1 Delta Degrees of Freedom. The divisor used in calculations is N - ddof, where N represents the number of elements. @@ -10747,7 +10747,7 @@ def _doc_parms(cls): Returns ------- -%(name1)s or %(name2)s (if level specified) +{name1} or {name2} (if level specified) Notes ----- @@ -11455,8 +11455,7 @@ def stat_func( def _make_stat_function_ddof( cls, name: str, name1: str, name2: str, axis_descr: str, desc: str, func: Callable ) -> Callable: - @Substitution(desc=desc, name1=name1, name2=name2, axis_descr=axis_descr) - @Appender(_num_ddof_doc) + @doc(_num_ddof_doc, desc=desc, name1=name1, name2=name2, axis_descr=axis_descr) def stat_func( self, axis=None, skipna=None, level=None, ddof=1, numeric_only=None, **kwargs ): From caeebac39fca9aabb37efa2156a15960a5594040 Mon Sep 17 00:00:00 2001 From: David Mrva Date: Fri, 22 May 2020 22:39:08 +0100 Subject: [PATCH 02/20] replaced the remaining occurrences of Appender in generic.py --- pandas/core/generic.py | 90 ++++++++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 38 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index ad04e5d9f08c9..b8e67222919f9 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -55,8 +55,6 @@ from pandas.compat.numpy import function as nv from pandas.errors import AbstractMethodError, InvalidIndexError from pandas.util._decorators import ( - Appender, - Substitution, doc, rewrite_axis_style_signature, ) @@ -6966,6 +6964,22 @@ def interpolate( 3 16.0 Name: d, dtype: float64 """ + + @doc(_shared_docs["interpolate"], **_shared_doc_kwargs) + def interpolate( + self, + method="linear", + axis=0, + limit=None, + inplace=False, + limit_direction="forward", + limit_area=None, + downcast=None, + **kwargs, + ): + """ + Interpolate values according to different methods. + """ inplace = validate_bool_kwarg(inplace, "inplace") axis = self._get_axis_number(axis) @@ -10701,29 +10715,29 @@ def _doc_parms(cls): _num_doc = """ -%(desc)s +{desc} Parameters ---------- -axis : %(axis_descr)s +axis : {axis_descr} Axis for the function to be applied on. skipna : bool, default True Exclude NA/null values when computing the result. level : int or level name, default None If the axis is a MultiIndex (hierarchical), count along a - particular level, collapsing into a %(name1)s. + particular level, collapsing into a {name1}. numeric_only : bool, default None Include only float, int, boolean columns. If None, will attempt to use everything, then use only numeric data. Not implemented for Series. -%(min_count)s\ +{min_count}\ **kwargs Additional keyword arguments to be passed to the function. Returns ------- -%(name1)s or %(name2)s (if level specified)\ -%(see_also)s\ -%(examples)s +{name1} or {name2} (if level specified)\ +{see_also}\ +{examples} """ _num_ddof_doc = """ @@ -10755,11 +10769,11 @@ def _doc_parms(cls): default `ddof=1`)\n""" _bool_doc = """ -%(desc)s +{desc} Parameters ---------- -axis : {0 or 'index', 1 or 'columns', None}, default 0 +axis : {{0 or 'index', 1 or 'columns', None}}, default 0 Indicate which axis or axes should be reduced. * 0 / 'index' : reduce the index, return a Series whose index is the @@ -10773,24 +10787,24 @@ def _doc_parms(cls): then use only boolean data. Not implemented for Series. skipna : bool, default True Exclude NA/null values. If the entire row/column is NA and skipna is - True, then the result will be %(empty_value)s, as for an empty row/column. + True, then the result will be {empty_value}, as for an empty row/column. If skipna is False, then NA are treated as True, because these are not equal to zero. level : int or level name, default None If the axis is a MultiIndex (hierarchical), count along a - particular level, collapsing into a %(name1)s. + particular level, collapsing into a {name1}. **kwargs : any, default None Additional keywords have no effect but might be accepted for compatibility with NumPy. Returns ------- -%(name1)s or %(name2)s - If level is specified, then, %(name2)s is returned; otherwise, %(name1)s +{name1} or {name2} + If level is specified, then, {name2} is returned; otherwise, {name1} is returned. -%(see_also)s -%(examples)s""" +{see_also} +{examples}""" _all_desc = """\ Return whether all elements are True, potentially over an axis. @@ -10853,14 +10867,14 @@ def _doc_parms(cls): """ _cnum_doc = """ -Return cumulative %(desc)s over a DataFrame or Series axis. +Return cumulative {desc} over a DataFrame or Series axis. Returns a DataFrame or Series of the same size containing the cumulative -%(desc)s. +{desc}. Parameters ---------- -axis : {0 or 'index', 1 or 'columns'}, default 0 +axis : {{0 or 'index', 1 or 'columns'}}, default 0 The index or the name of the axis. 0 is equivalent to None or 'index'. skipna : bool, default True Exclude NA/null values. If an entire row/column is NA, the result @@ -10871,21 +10885,21 @@ def _doc_parms(cls): Returns ------- -%(name1)s or %(name2)s - Return cumulative %(desc)s of %(name1)s or %(name2)s. +{name1} or {name2} + Return cumulative {desc} of {name1} or {name2}. See Also -------- -core.window.Expanding.%(accum_func_name)s : Similar functionality +core.window.Expanding.{accum_func_name} : Similar functionality but ignores ``NaN`` values. -%(name2)s.%(accum_func_name)s : Return the %(desc)s over - %(name2)s axis. -%(name2)s.cummax : Return cumulative maximum over %(name2)s axis. -%(name2)s.cummin : Return cumulative minimum over %(name2)s axis. -%(name2)s.cumsum : Return cumulative sum over %(name2)s axis. -%(name2)s.cumprod : Return cumulative product over %(name2)s axis. +{name2}.{accum_func_name} : Return the {desc} over + {name2} axis. +{name2}.cummax : Return cumulative maximum over {name2} axis. +{name2}.cummin : Return cumulative minimum over {name2} axis. +{name2}.cumsum : Return cumulative sum over {name2} axis. +{name2}.cumprod : Return cumulative product over {name2} axis. -%(examples)s""" +{examples}""" _cummin_examples = """\ Examples @@ -11366,7 +11380,8 @@ def _make_min_count_stat_function( see_also: str = "", examples: str = "", ) -> Callable: - @Substitution( + @doc( + _num_doc, desc=desc, name1=name1, name2=name2, @@ -11375,7 +11390,6 @@ def _make_min_count_stat_function( see_also=see_also, examples=examples, ) - @Appender(_num_doc) def stat_func( self, axis=None, @@ -11422,7 +11436,8 @@ def _make_stat_function( see_also: str = "", examples: str = "", ) -> Callable: - @Substitution( + @doc( + _num_doc, desc=desc, name1=name1, name2=name2, @@ -11431,7 +11446,6 @@ def _make_stat_function( see_also=see_also, examples=examples, ) - @Appender(_num_doc) def stat_func( self, axis=None, skipna=None, level=None, numeric_only=None, **kwargs ): @@ -11486,7 +11500,8 @@ def _make_cum_function( accum_func_name: str, examples: str, ) -> Callable: - @Substitution( + @doc( + _cnum_doc, desc=desc, name1=name1, name2=name2, @@ -11494,7 +11509,6 @@ def _make_cum_function( accum_func_name=accum_func_name, examples=examples, ) - @Appender(_cnum_doc) def cum_func(self, axis=None, skipna=True, *args, **kwargs): skipna = nv.validate_cum_func_with_skipna(skipna, args, kwargs, name) if axis is None: @@ -11532,7 +11546,8 @@ def _make_logical_function( examples: str, empty_value: bool, ) -> Callable: - @Substitution( + @doc( + _bool_doc, desc=desc, name1=name1, name2=name2, @@ -11541,7 +11556,6 @@ def _make_logical_function( examples=examples, empty_value=empty_value, ) - @Appender(_bool_doc) def logical_func(self, axis=0, bool_only=None, skipna=True, level=None, **kwargs): nv.validate_logical_func(tuple(), kwargs, fname=name) if level is not None: From aded5128f43b09334fbe1dd25214211bc4fdeb4d Mon Sep 17 00:00:00 2001 From: David Mrva Date: Fri, 22 May 2020 23:21:52 +0100 Subject: [PATCH 03/20] removed the remaining occurrences of Substitution in generic.py --- pandas/core/generic.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index b8e67222919f9..d406b0a0141ff 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -2973,7 +2973,7 @@ class (index) object 'bird' 'bird' 'mammal' 'mammal' else: return xarray.Dataset.from_dataframe(self) - @Substitution(returns=fmt.return_docstring) + @doc(returns=fmt.return_docstring) def to_latex( self, buf=None, @@ -3002,9 +3002,9 @@ def to_latex( r""" Render object to a LaTeX tabular, longtable, or nested table/tabular. - Requires ``\usepackage{booktabs}``. The output can be copy/pasted + Requires ``\usepackage{{booktabs}}``. The output can be copy/pasted into a main LaTeX document or read from an external file - with ``\input{table.tex}``. + with ``\input{{table.tex}}``. .. versionchanged:: 0.20.2 Added to Series. @@ -3027,13 +3027,13 @@ def to_latex( Write row names (index). na_rep : str, default 'NaN' Missing data representation. - formatters : list of functions or dict of {str: function}, optional + formatters : list of functions or dict of {{str: function}}, optional Formatter functions to apply to columns' elements by position or name. The result of each function must be a unicode string. List must be of length equal to the number of columns. float_format : one-parameter function or str, optional, default None Formatter for floating point numbers. For example - ``float_format="%%.2f"`` and ``float_format="{:0.2f}".format`` will + ``float_format="%.2f"`` and ``float_format="{{:0.2f}}".format`` will both result in 0.1234 being formatted as 0.12. sparsify : bool, optional Set to False for a DataFrame with a hierarchical index to print @@ -3051,7 +3051,7 @@ def to_latex( longtable : bool, optional By default, the value will be read from the pandas config module. Use a longtable environment instead of tabular. Requires - adding a \usepackage{longtable} to your LaTeX preamble. + adding a \usepackage{{longtable}} to your LaTeX preamble. escape : bool, optional By default, the value will be read from the pandas config module. When set to False prevents from escaping latex special @@ -3069,24 +3069,24 @@ def to_latex( The default will be read from the config module. multirow : bool, default False Use \multirow to enhance MultiIndex rows. Requires adding a - \usepackage{multirow} to your LaTeX preamble. Will print + \usepackage{{multirow}} to your LaTeX preamble. Will print centered labels (instead of top-aligned) across the contained rows, separating groups via clines. The default will be read from the pandas config module. caption : str, optional - The LaTeX caption to be placed inside ``\caption{}`` in the output. + The LaTeX caption to be placed inside ``\caption{{}}`` in the output. .. versionadded:: 1.0.0 label : str, optional - The LaTeX label to be placed inside ``\label{}`` in the output. - This is used with ``\ref{}`` in the main ``.tex`` file. + The LaTeX label to be placed inside ``\label{{}}`` in the output. + This is used with ``\ref{{}}`` in the main ``.tex`` file. .. versionadded:: 1.0.0 position : str, optional The LaTeX positional argument for tables, to be placed after ``\begin{}`` in the output. - %(returns)s + {returns} See Also -------- DataFrame.to_string : Render a DataFrame to a console-friendly @@ -3095,18 +3095,18 @@ def to_latex( Examples -------- - >>> df = pd.DataFrame({'name': ['Raphael', 'Donatello'], + >>> df = pd.DataFrame({{'name': ['Raphael', 'Donatello'], ... 'mask': ['red', 'purple'], - ... 'weapon': ['sai', 'bo staff']}) + ... 'weapon': ['sai', 'bo staff']}}) >>> print(df.to_latex(index=False)) # doctest: +NORMALIZE_WHITESPACE - \begin{tabular}{lll} + \begin{{tabular}}{{lll}} \toprule name & mask & weapon \\ \midrule Raphael & red & sai \\ Donatello & purple & bo staff \\ \bottomrule - \end{tabular} + \end{{tabular}} """ # Get defaults from the pandas config if self.ndim == 1: From d81879b9ddcfbf5dc3b367536c4ea0e9150008b0 Mon Sep 17 00:00:00 2001 From: David Mrva Date: Sat, 23 May 2020 16:06:04 +0100 Subject: [PATCH 04/20] fixed a doc string format --- pandas/core/generic.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index d406b0a0141ff..44f3869e8fa12 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -10276,10 +10276,10 @@ def pct_change( Percentage change in French franc, Deutsche Mark, and Italian lira from 1980-01-01 to 1980-03-01. - >>> df = pd.DataFrame({ + >>> df = pd.DataFrame({{ ... 'FR': [4.0405, 4.0963, 4.3149], ... 'GR': [1.7246, 1.7482, 1.8519], - ... 'IT': [804.74, 810.01, 860.13]}, + ... 'IT': [804.74, 810.01, 860.13]}}, ... index=['1980-01-01', '1980-02-01', '1980-03-01']) >>> df FR GR IT @@ -10296,10 +10296,10 @@ def pct_change( Percentage of change in GOOG and APPL stock volume. Shows computing the percentage change between columns. - >>> df = pd.DataFrame({ + >>> df = pd.DataFrame({{ ... '2016': [1769950, 30586265], ... '2015': [1500923, 40912316], - ... '2014': [1371819, 41403351]}, + ... '2014': [1371819, 41403351]}}, ... index=['GOOG', 'APPL']) >>> df 2016 2015 2014 From f0afb4d222608f839cbcdecd97455bc277fc6a81 Mon Sep 17 00:00:00 2001 From: David Mrva Date: Sat, 23 May 2020 17:09:40 +0100 Subject: [PATCH 05/20] fixed a doc string format --- pandas/core/generic.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 44f3869e8fa12..f0a5582b5775b 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -7267,11 +7267,11 @@ def isna(self: FrameOrSeries) -> FrameOrSeries: -------- Show which entries in a DataFrame are NA. - >>> df = pd.DataFrame({{'age': [5, 6, np.NaN], - ... 'born': [pd.NaT, pd.Timestamp('1939-05-27'), + >>> df = pd.DataFrame(dict(age=[5, 6, np.NaN], + ... born=[pd.NaT, pd.Timestamp('1939-05-27'), ... pd.Timestamp('1940-04-25')], - ... 'name': ['Alfred', 'Batman', ''], - ... 'toy': [None, 'Batmobile', 'Joker']}}) + ... name=['Alfred', 'Batman', ''], + ... toy=[None, 'Batmobile', 'Joker'])) >>> df age born name toy 0 5.0 NaT Alfred None From 6d2dc5ddfbc29214c72407520eb589e155a2b3cb Mon Sep 17 00:00:00 2001 From: David Mrva Date: Sat, 23 May 2020 18:36:43 +0100 Subject: [PATCH 06/20] fixed a doc string format --- pandas/core/generic.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index f0a5582b5775b..3fa2034418aca 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -7334,11 +7334,11 @@ def notna(self: FrameOrSeries) -> FrameOrSeries: -------- Show which entries in a DataFrame are not NA. - >>> df = pd.DataFrame({{'age': [5, 6, np.NaN], - ... 'born': [pd.NaT, pd.Timestamp('1939-05-27'), + >>> df = pd.DataFrame(dict(age=[5, 6, np.NaN], + ... born=[pd.NaT, pd.Timestamp('1939-05-27'), ... pd.Timestamp('1940-04-25')], - ... 'name': ['Alfred', 'Batman', ''], - ... 'toy': [None, 'Batmobile', 'Joker']}}) + ... name=['Alfred', 'Batman', ''], + ... toy=[None, 'Batmobile', 'Joker'])) >>> df age born name toy 0 5.0 NaT Alfred None From dba2ca6662d0644a7c609bf6308307eacb5daff0 Mon Sep 17 00:00:00 2001 From: David Mrva Date: Sun, 24 May 2020 15:01:46 +0100 Subject: [PATCH 07/20] a fix for a sphinx complaint --- pandas/core/generic.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 3fa2034418aca..dda6d6edf0b0e 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -6797,6 +6797,7 @@ def interpolate( `scipy.interpolate.BPoly.from_derivatives` which replaces 'piecewise_polynomial' interpolation method in scipy 0.18. + axis : {{0 or 'index', 1 or 'columns', None}}, default None Axis to interpolate along. limit : int, optional From ea4bc561d659e02e68905abc98c16c1d941f55b8 Mon Sep 17 00:00:00 2001 From: David Mrva Date: Wed, 27 May 2020 21:44:46 +0100 Subject: [PATCH 08/20] stop a sphinx warning --- pandas/core/generic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index dda6d6edf0b0e..0fdd331ff10db 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -6836,7 +6836,7 @@ def interpolate( downcast : optional, 'infer' or None, defaults to None Downcast dtypes if possible. - **kwargs + ``**kwargs`` Keyword arguments to pass on to the interpolating function. Returns From 084f7ebc401f4947dbe937a3a9d56698fb34d03c Mon Sep 17 00:00:00 2001 From: David Mrva Date: Mon, 27 Jul 2020 21:15:52 +0100 Subject: [PATCH 09/20] fixed a missing import --- pandas/core/generic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 0fdd331ff10db..bf06382e550e1 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -56,7 +56,7 @@ from pandas.errors import AbstractMethodError, InvalidIndexError from pandas.util._decorators import ( doc, - rewrite_axis_style_signature, + rewrite_axis_style_signature, Appender, ) from pandas.util._validators import ( validate_bool_kwarg, From 729b711af058ae23f41bff79472946d5c25d46b2 Mon Sep 17 00:00:00 2001 From: David Mrva Date: Mon, 27 Jul 2020 21:46:46 +0100 Subject: [PATCH 10/20] fixed linting and a doctest --- pandas/core/generic.py | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index bf06382e550e1..e9fd1518f2d40 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -1,7 +1,6 @@ from __future__ import annotations import collections -from datetime import timedelta import functools import gc from io import StringIO @@ -26,13 +25,14 @@ Union, cast, ) -import warnings -import weakref import numpy as np +import pandas as pd +import pandas.core.algorithms as algos +import pandas.core.common as com +import pandas.core.indexing as indexing from pandas._config import config - from pandas._libs import lib from pandas._libs.tslibs import Tick, Timestamp, to_offset from pandas._typing import ( @@ -53,17 +53,9 @@ from pandas.compat import set_function_name from pandas.compat._optional import import_optional_dependency from pandas.compat.numpy import function as nv -from pandas.errors import AbstractMethodError, InvalidIndexError -from pandas.util._decorators import ( - doc, - rewrite_axis_style_signature, Appender, -) -from pandas.util._validators import ( - validate_bool_kwarg, - validate_fillna_kwargs, - validate_percentile, -) - +from pandas.core import missing, nanops +from pandas.core.base import PandasObject, SelectionMixin +from pandas.core.construction import create_series_with_explicit_dtype from pandas.core.dtypes.common import ( ensure_int64, ensure_object, @@ -98,7 +90,6 @@ from pandas.core.indexes.api import Index, MultiIndex, RangeIndex, ensure_index from pandas.core.indexes.datetimes import DatetimeIndex from pandas.core.indexes.period import Period, PeriodIndex -import pandas.core.indexing as indexing from pandas.core.internals import BlockManager from pandas.core.missing import find_valid_index from pandas.core.ops import align_method_FRAME From 4bacf07f2efbc9c9a80ced07c5d5a3e5d5befe58 Mon Sep 17 00:00:00 2001 From: David Mrva Date: Sat, 12 Sep 2020 12:40:36 +0100 Subject: [PATCH 11/20] resolved rebase issues --- pandas/core/generic.py | 110 +---------------------------------------- 1 file changed, 1 insertion(+), 109 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index e9fd1518f2d40..0aa26c68292b9 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -28,9 +28,6 @@ import numpy as np -import pandas as pd -import pandas.core.algorithms as algos -import pandas.core.common as com import pandas.core.indexing as indexing from pandas._config import config from pandas._libs import lib @@ -53,9 +50,6 @@ from pandas.compat import set_function_name from pandas.compat._optional import import_optional_dependency from pandas.compat.numpy import function as nv -from pandas.core import missing, nanops -from pandas.core.base import PandasObject, SelectionMixin -from pandas.core.construction import create_series_with_explicit_dtype from pandas.core.dtypes.common import ( ensure_int64, ensure_object, @@ -99,6 +93,7 @@ from pandas.io.formats import format as fmt from pandas.io.formats.format import DataFrameFormatter, format_percentiles from pandas.io.formats.printing import pprint_thing +from pandas.util._decorators import doc if TYPE_CHECKING: from pandas._libs.tslibs import BaseOffset @@ -6957,109 +6952,6 @@ def interpolate( Name: d, dtype: float64 """ - @doc(_shared_docs["interpolate"], **_shared_doc_kwargs) - def interpolate( - self, - method="linear", - axis=0, - limit=None, - inplace=False, - limit_direction="forward", - limit_area=None, - downcast=None, - **kwargs, - ): - """ - Interpolate values according to different methods. - """ - inplace = validate_bool_kwarg(inplace, "inplace") - - axis = self._get_axis_number(axis) - - fillna_methods = ["ffill", "bfill", "pad", "backfill"] - should_transpose = axis == 1 and method not in fillna_methods - - obj = self.T if should_transpose else self - - if obj.empty: - return self.copy() - - if method not in fillna_methods: - axis = self._info_axis_number - - if isinstance(obj.index, MultiIndex) and method != "linear": - raise ValueError( - "Only `method=linear` interpolation is supported on MultiIndexes." - ) - - # Set `limit_direction` depending on `method` - if limit_direction is None: - limit_direction = ( - "backward" if method in ("backfill", "bfill") else "forward" - ) - else: - if method in ("pad", "ffill") and limit_direction != "forward": - raise ValueError( - f"`limit_direction` must be 'forward' for method `{method}`" - ) - if method in ("backfill", "bfill") and limit_direction != "backward": - raise ValueError( - f"`limit_direction` must be 'backward' for method `{method}`" - ) - - if obj.ndim == 2 and np.all(obj.dtypes == np.dtype(object)): - raise TypeError( - "Cannot interpolate with all object-dtype columns " - "in the DataFrame. Try setting at least one " - "column to a numeric dtype." - ) - - # create/use the index - if method == "linear": - # prior default - index = np.arange(len(obj.index)) - else: - index = obj.index - methods = {"index", "values", "nearest", "time"} - is_numeric_or_datetime = ( - is_numeric_dtype(index.dtype) - or is_datetime64_any_dtype(index.dtype) - or is_timedelta64_dtype(index.dtype) - ) - if method not in methods and not is_numeric_or_datetime: - raise ValueError( - "Index column must be numeric or datetime type when " - f"using {method} method other than linear. " - "Try setting a numeric or datetime index column before " - "interpolating." - ) - - if isna(index).any(): - raise NotImplementedError( - "Interpolation with NaNs in the index " - "has not been implemented. Try filling " - "those NaNs before interpolating." - ) - new_data = obj._mgr.interpolate( - method=method, - axis=axis, - index=index, - limit=limit, - limit_direction=limit_direction, - limit_area=limit_area, - inplace=inplace, - downcast=downcast, - **kwargs, - ) - - result = self._constructor(new_data) - if should_transpose: - result = result.T - if inplace: - return self._update_inplace(result) - else: - return result.__finalize__(self, method="interpolate") - # ---------------------------------------------------------------------- # Timeseries methods Methods From 3e901c6c9a1cc146bcf0cd0db2d69eac7f36ef2e Mon Sep 17 00:00:00 2001 From: David Mrva Date: Sat, 12 Sep 2020 14:40:19 +0100 Subject: [PATCH 12/20] resolved rebase issues --- pandas/core/generic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 0aa26c68292b9..9dc19c364f8f8 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -93,7 +93,7 @@ from pandas.io.formats import format as fmt from pandas.io.formats.format import DataFrameFormatter, format_percentiles from pandas.io.formats.printing import pprint_thing -from pandas.util._decorators import doc +from pandas.util._decorators import doc, rewrite_axis_style_signature, Appender if TYPE_CHECKING: from pandas._libs.tslibs import BaseOffset @@ -3071,7 +3071,7 @@ def to_latex( .. versionadded:: 1.0.0 position : str, optional The LaTeX positional argument for tables, to be placed after - ``\begin{}`` in the output. + ``\begin{{}}`` in the output. {returns} See Also -------- From 7089936e28166c6c7ae638d134003bec84eab9b2 Mon Sep 17 00:00:00 2001 From: David Mrva Date: Sat, 12 Sep 2020 15:47:54 +0100 Subject: [PATCH 13/20] resolved rebase issues --- pandas/core/generic.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 9dc19c364f8f8..50d3a03c1caad 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3,6 +3,9 @@ import collections import functools import gc +import warnings +import weakref +from datetime import timedelta from io import StringIO import json import operator @@ -89,11 +92,13 @@ from pandas.core.ops import align_method_FRAME from pandas.core.shared_docs import _shared_docs from pandas.core.window import Expanding, ExponentialMovingWindow, Rolling, Window +from pandas.errors import AbstractMethodError, InvalidIndexError from pandas.io.formats import format as fmt from pandas.io.formats.format import DataFrameFormatter, format_percentiles from pandas.io.formats.printing import pprint_thing from pandas.util._decorators import doc, rewrite_axis_style_signature, Appender +from pandas.util._validators import validate_bool_kwarg, validate_fillna_kwargs, validate_percentile if TYPE_CHECKING: from pandas._libs.tslibs import BaseOffset From 66781277587dd4c49ab0564d051886610dd490ea Mon Sep 17 00:00:00 2001 From: David Mrva Date: Sat, 12 Sep 2020 16:08:43 +0100 Subject: [PATCH 14/20] fixed a line too long --- pandas/core/generic.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 50d3a03c1caad..36b0673826978 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -98,7 +98,8 @@ from pandas.io.formats.format import DataFrameFormatter, format_percentiles from pandas.io.formats.printing import pprint_thing from pandas.util._decorators import doc, rewrite_axis_style_signature, Appender -from pandas.util._validators import validate_bool_kwarg, validate_fillna_kwargs, validate_percentile +from pandas.util._validators import validate_bool_kwarg, validate_fillna_kwargs,\ + validate_percentile if TYPE_CHECKING: from pandas._libs.tslibs import BaseOffset From b5cc2cca584caeca0c1e7823cd45512cf4201f06 Mon Sep 17 00:00:00 2001 From: David Mrva Date: Sat, 12 Sep 2020 16:27:14 +0100 Subject: [PATCH 15/20] fixed the import order --- pandas/core/generic.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 36b0673826978..b6a95081e1e6c 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -1,11 +1,9 @@ from __future__ import annotations import collections +from datetime import timedelta import functools import gc -import warnings -import weakref -from datetime import timedelta from io import StringIO import json import operator @@ -28,11 +26,13 @@ Union, cast, ) +import warnings +import weakref import numpy as np -import pandas.core.indexing as indexing from pandas._config import config + from pandas._libs import lib from pandas._libs.tslibs import Tick, Timestamp, to_offset from pandas._typing import ( @@ -53,6 +53,14 @@ from pandas.compat import set_function_name from pandas.compat._optional import import_optional_dependency from pandas.compat.numpy import function as nv +from pandas.errors import AbstractMethodError, InvalidIndexError +from pandas.util._decorators import Appender, doc, rewrite_axis_style_signature +from pandas.util._validators import ( + validate_bool_kwarg, + validate_fillna_kwargs, + validate_percentile, +) + from pandas.core.dtypes.common import ( ensure_int64, ensure_object, @@ -87,19 +95,16 @@ from pandas.core.indexes.api import Index, MultiIndex, RangeIndex, ensure_index from pandas.core.indexes.datetimes import DatetimeIndex from pandas.core.indexes.period import Period, PeriodIndex +import pandas.core.indexing as indexing from pandas.core.internals import BlockManager from pandas.core.missing import find_valid_index from pandas.core.ops import align_method_FRAME from pandas.core.shared_docs import _shared_docs from pandas.core.window import Expanding, ExponentialMovingWindow, Rolling, Window -from pandas.errors import AbstractMethodError, InvalidIndexError from pandas.io.formats import format as fmt from pandas.io.formats.format import DataFrameFormatter, format_percentiles from pandas.io.formats.printing import pprint_thing -from pandas.util._decorators import doc, rewrite_axis_style_signature, Appender -from pandas.util._validators import validate_bool_kwarg, validate_fillna_kwargs,\ - validate_percentile if TYPE_CHECKING: from pandas._libs.tslibs import BaseOffset From cbcc6fa5819d212fdc21e6a304bb835bf70c9d80 Mon Sep 17 00:00:00 2001 From: David Mrva Date: Sun, 13 Sep 2020 08:52:20 +0100 Subject: [PATCH 16/20] resolved rebase issue --- pandas/core/generic.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index b6a95081e1e6c..2de1321c774dd 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3092,9 +3092,9 @@ def to_latex( Examples -------- - >>> df = pd.DataFrame({{'name': ['Raphael', 'Donatello'], - ... 'mask': ['red', 'purple'], - ... 'weapon': ['sai', 'bo staff']}}) + >>> df = pd.DataFrame(dict(name=['Raphael', 'Donatello'], + ... mask=['red', 'purple'], + ... weapon=['sai', 'bo staff'])) >>> print(df.to_latex(index=False)) # doctest: +NORMALIZE_WHITESPACE \begin{{tabular}}{{lll}} \toprule From a27e3f566b97bc5828b2b8ec30a2336659a1f658 Mon Sep 17 00:00:00 2001 From: David Mrva Date: Sun, 13 Sep 2020 08:52:36 +0100 Subject: [PATCH 17/20] resolved rebase issue --- pandas/core/generic.py | 87 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 2de1321c774dd..1fc129c35a66f 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -6962,6 +6962,93 @@ def interpolate( 3 16.0 Name: d, dtype: float64 """ + inplace = validate_bool_kwarg(inplace, "inplace") + + axis = self._get_axis_number(axis) + + fillna_methods = ["ffill", "bfill", "pad", "backfill"] + should_transpose = axis == 1 and method not in fillna_methods + + obj = self.T if should_transpose else self + + if obj.empty: + return self.copy() + + if method not in fillna_methods: + axis = self._info_axis_number + + if isinstance(obj.index, MultiIndex) and method != "linear": + raise ValueError( + "Only `method=linear` interpolation is supported on MultiIndexes." + ) + + # Set `limit_direction` depending on `method` + if limit_direction is None: + limit_direction = ( + "backward" if method in ("backfill", "bfill") else "forward" + ) + else: + if method in ("pad", "ffill") and limit_direction != "forward": + raise ValueError( + f"`limit_direction` must be 'forward' for method `{method}`" + ) + if method in ("backfill", "bfill") and limit_direction != "backward": + raise ValueError( + f"`limit_direction` must be 'backward' for method `{method}`" + ) + + if obj.ndim == 2 and np.all(obj.dtypes == np.dtype(object)): + raise TypeError( + "Cannot interpolate with all object-dtype columns " + "in the DataFrame. Try setting at least one " + "column to a numeric dtype." + ) + + # create/use the index + if method == "linear": + # prior default + index = np.arange(len(obj.index)) + else: + index = obj.index + methods = {"index", "values", "nearest", "time"} + is_numeric_or_datetime = ( + is_numeric_dtype(index.dtype) + or is_datetime64_any_dtype(index.dtype) + or is_timedelta64_dtype(index.dtype) + ) + if method not in methods and not is_numeric_or_datetime: + raise ValueError( + "Index column must be numeric or datetime type when " + f"using {method} method other than linear. " + "Try setting a numeric or datetime index column before " + "interpolating." + ) + + if isna(index).any(): + raise NotImplementedError( + "Interpolation with NaNs in the index " + "has not been implemented. Try filling " + "those NaNs before interpolating." + ) + new_data = obj._mgr.interpolate( + method=method, + axis=axis, + index=index, + limit=limit, + limit_direction=limit_direction, + limit_area=limit_area, + inplace=inplace, + downcast=downcast, + **kwargs, + ) + + result = self._constructor(new_data) + if should_transpose: + result = result.T + if inplace: + return self._update_inplace(result) + else: + return result.__finalize__(self, method="interpolate") # ---------------------------------------------------------------------- # Timeseries methods Methods From 14e4b99b62983c93cb24504362d1f77c180385e9 Mon Sep 17 00:00:00 2001 From: David Mrva Date: Sun, 13 Sep 2020 09:32:51 +0100 Subject: [PATCH 18/20] fixed formatting --- pandas/core/generic.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 1fc129c35a66f..1f842c588ee70 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -7012,9 +7012,9 @@ def interpolate( index = obj.index methods = {"index", "values", "nearest", "time"} is_numeric_or_datetime = ( - is_numeric_dtype(index.dtype) - or is_datetime64_any_dtype(index.dtype) - or is_timedelta64_dtype(index.dtype) + is_numeric_dtype(index.dtype) + or is_datetime64_any_dtype(index.dtype) + or is_timedelta64_dtype(index.dtype) ) if method not in methods and not is_numeric_or_datetime: raise ValueError( From 26105490eda9814f31fbc86c847bee7fe2a39e89 Mon Sep 17 00:00:00 2001 From: David Mrva Date: Sun, 13 Sep 2020 20:08:41 +0100 Subject: [PATCH 19/20] fixed doc tests --- pandas/core/generic.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 1f842c588ee70..f2400c2687e77 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -10258,10 +10258,10 @@ def pct_change( Percentage change in French franc, Deutsche Mark, and Italian lira from 1980-01-01 to 1980-03-01. - >>> df = pd.DataFrame({{ - ... 'FR': [4.0405, 4.0963, 4.3149], - ... 'GR': [1.7246, 1.7482, 1.8519], - ... 'IT': [804.74, 810.01, 860.13]}}, + >>> df = pd.DataFrame(dict( + ... FR=[4.0405, 4.0963, 4.3149], + ... GR=[1.7246, 1.7482, 1.8519], + ... IT=[804.74, 810.01, 860.13]), ... index=['1980-01-01', '1980-02-01', '1980-03-01']) >>> df FR GR IT @@ -10278,10 +10278,10 @@ def pct_change( Percentage of change in GOOG and APPL stock volume. Shows computing the percentage change between columns. - >>> df = pd.DataFrame({{ - ... '2016': [1769950, 30586265], - ... '2015': [1500923, 40912316], - ... '2014': [1371819, 41403351]}}, + >>> df = pd.DataFrame(dict([ + ... ('2016', [1769950, 30586265]), + ... ('2015', [1500923, 40912316]), + ... ('2014', [1371819, 41403351])]), ... index=['GOOG', 'APPL']) >>> df 2016 2015 2014 From 54505a80b3b09b53e27ab1bf17f85cddc1312e14 Mon Sep 17 00:00:00 2001 From: David Mrva Date: Sun, 13 Sep 2020 20:37:45 +0100 Subject: [PATCH 20/20] fix a numpydoc validation error requiring parameter types --- pandas/core/generic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index f2400c2687e77..f0b690bc56d04 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -6833,7 +6833,7 @@ def interpolate( downcast : optional, 'infer' or None, defaults to None Downcast dtypes if possible. - ``**kwargs`` + ``**kwargs`` : optional Keyword arguments to pass on to the interpolating function. Returns