From bab3a2b35fa8f0e7384f5f22503f51c42a318f32 Mon Sep 17 00:00:00 2001 From: math-and-data Date: Thu, 20 Sep 2018 00:53:01 +0200 Subject: [PATCH 01/16] DOC: Improve the docstring of DataFrame.reindex_like() --- pandas/core/frame.py | 12 ++++++++ pandas/core/generic.py | 68 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 68 insertions(+), 12 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index bb221ced9e6bd..52efebd1b86ea 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -3842,6 +3842,12 @@ def set_index(self, keys, drop=True, append=False, inplace=False, necessary. Setting to False will improve the performance of this method + See Also + -------- + DataFrame.reset_index : opposite of set_index + DataFrame.reindex : change to new indices or expand indices + DataFrame.reindex_like : change to same indices as other DataFrame + Examples -------- >>> df = pd.DataFrame({'month': [1, 4, 7, 10], @@ -3980,6 +3986,12 @@ def reset_index(self, level=None, drop=False, inplace=False, col_level=0, Returns ------- resetted : DataFrame + + See Also + -------- + DataFrame.set_index : opposite of reset_index + DataFrame.reindex : change to new indices or expand indices + DataFrame.reindex_like : change to same indices as other DataFrame Examples -------- diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 3f7334131e146..ed0ede7106c89 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3331,12 +3331,18 @@ def select(self, crit, axis=0): def reindex_like(self, other, method=None, copy=True, limit=None, tolerance=None): - """Return an object with matching indices to myself. + """ + Return an object with matching indices as other object. + + Conform the object to the same index on all axes. Optional filling logic, + placing NA/NaN in locations having no value in the previous index. + A new object is produced unless the new index is equivalent to the + current one and copy=False. Parameters ---------- other : Object - method : string or None + method : {None, 'backfill'/'bfill', 'pad'/'ffill', 'nearest'} copy : boolean, default True limit : int, default None Maximum number of consecutive labels to fill for inexact matches. @@ -3348,12 +3354,41 @@ def reindex_like(self, other, method=None, copy=True, limit=None, Notes ----- - Like calling s.reindex(index=other.index, columns=other.columns, - method=...) + Like calling `.reindex(index=other.index, columns=other.columns, + method=...)` Returns ------- - reindexed : same as input + Object + same object type as input object, but with changed indices on each axis + + See Also + -------- + DataFrame.set_index : set row labels + DataFrame.reset_index : remove row labels or move them to new columns + DataFrame.reindex : change to new indices or expand indices + + Examples + -------- + >>> df_weather_station_1 = pd.DataFrame([[24.3, 75.7, 'high'], + ... [31, 87.8, 'high'], + ... [22, 71.6, 'medium'], + ... [35, 95, 'medium']], + ... columns=['temp_celsius', 'temp_fahrenheit', 'windspeed'], + ... index=pd.date_range(start='2014-02-12', end='2014-02-15', freq='D')) + + >>> df_weather_station_2 = pd.DataFrame([[28, 'low'], + ... [30, 'low'], + ... [35.1, 'medium']], + ... columns=['temp_celsius', 'windspeed'], + ... index=pd.DatetimeIndex(['2014-02-12', '2014-02-13', '2014-02-15'])) + + >>> df_weather_station_2.reindex_like(df_weather_station_1) + temp_celsius temp_fahrenheit windspeed + 2014-02-12 28.0 NaN low + 2014-02-13 30.0 NaN low + 2014-02-14 NaN NaN NaN + 2014-02-15 35.1 NaN medium """ d = other._construct_axes_dict(axes=self._AXIS_ORDERS, method=method, copy=copy, limit=limit, @@ -3769,6 +3804,12 @@ def sort_index(self, axis=0, level=None, ascending=True, inplace=False, .. versionadded:: 0.21.0 (list-like tolerance) + See Also + -------- + DataFrame.set_index : set row labels + DataFrame.reset_index : remove row labels or move them to new columns + DataFrame.reindex_like : change to same indices as other DataFrame + Examples -------- @@ -3994,8 +4035,7 @@ def _needs_reindex_multi(self, axes, method, level): def _reindex_multi(self, axes, copy, fill_value): return NotImplemented - _shared_docs[ - 'reindex_axis'] = ("""Conform input object to new index with optional + _shared_docs['reindex_axis'] = ("""Conform input object to new index with optional filling logic, placing NA/NaN in locations having no value in the previous index. A new object is produced unless the new index is equivalent to the current one and copy=False @@ -4035,17 +4075,21 @@ def _reindex_multi(self, axes, copy, fill_value): .. versionadded:: 0.21.0 (list-like tolerance) - Examples - -------- - >>> df.reindex_axis(['A', 'B', 'C'], axis=1) - See Also -------- - reindex, reindex_like + DataFrame.set_index : set row labels + DataFrame.reset_index : remove row labels or move them to new columns + DataFrame.reindex : change to new indices or expand indices + DataFrame.reindex_like : change to same indices as other DataFrame Returns ------- reindexed : %(klass)s + + Examples + -------- + + >>> df.reindex_axis(['A', 'B', 'C'], axis=1) """) @Appender(_shared_docs['reindex_axis'] % _shared_doc_kwargs) From cd3ec6273b9030a53e83743af8f1fcb09894351c Mon Sep 17 00:00:00 2001 From: math-and-data Date: Thu, 20 Sep 2018 01:00:36 +0200 Subject: [PATCH 02/16] DOC: Improve the docstring of DataFrame.reindex_like() --- pandas/core/frame.py | 2 +- pandas/core/generic.py | 25 +++++++++++++------------ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 52efebd1b86ea..85150ca9ae47c 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -3986,7 +3986,7 @@ def reset_index(self, level=None, drop=False, inplace=False, col_level=0, Returns ------- resetted : DataFrame - + See Also -------- DataFrame.set_index : opposite of reset_index diff --git a/pandas/core/generic.py b/pandas/core/generic.py index ed0ede7106c89..bcf4819d8a0d5 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3333,11 +3333,11 @@ def reindex_like(self, other, method=None, copy=True, limit=None, tolerance=None): """ Return an object with matching indices as other object. - - Conform the object to the same index on all axes. Optional filling logic, - placing NA/NaN in locations having no value in the previous index. - A new object is produced unless the new index is equivalent to the - current one and copy=False. + + Conform the object to the same index on all axes. Optional + filling logic, placing NA/NaN in locations having no value + in the previous index. A new object is produced unless the + new index is equivalent to the current one and copy=False. Parameters ---------- @@ -3360,7 +3360,7 @@ def reindex_like(self, other, method=None, copy=True, limit=None, Returns ------- Object - same object type as input object, but with changed indices on each axis + same object type as input, but with changed indices on each axis See Also -------- @@ -3369,19 +3369,21 @@ def reindex_like(self, other, method=None, copy=True, limit=None, DataFrame.reindex : change to new indices or expand indices Examples - -------- + -------- >>> df_weather_station_1 = pd.DataFrame([[24.3, 75.7, 'high'], ... [31, 87.8, 'high'], ... [22, 71.6, 'medium'], ... [35, 95, 'medium']], ... columns=['temp_celsius', 'temp_fahrenheit', 'windspeed'], - ... index=pd.date_range(start='2014-02-12', end='2014-02-15', freq='D')) + ... index=pd.date_range(start='2014-02-12', + ... end='2014-02-15', freq='D')) - >>> df_weather_station_2 = pd.DataFrame([[28, 'low'], + >>> df_weather_station_2 = pd.DataFrame([[28, 'low'], ... [30, 'low'], ... [35.1, 'medium']], - ... columns=['temp_celsius', 'windspeed'], - ... index=pd.DatetimeIndex(['2014-02-12', '2014-02-13', '2014-02-15'])) + ... columns=['temp_celsius', 'windspeed'], + ... index=pd.DatetimeIndex(['2014-02-12', '2014-02-13', + ... '2014-02-15'])) >>> df_weather_station_2.reindex_like(df_weather_station_1) temp_celsius temp_fahrenheit windspeed @@ -4088,7 +4090,6 @@ def _reindex_multi(self, axes, copy, fill_value): Examples -------- - >>> df.reindex_axis(['A', 'B', 'C'], axis=1) """) From 1ee27513099c95c497e6e90fb0937b75cfd7925b Mon Sep 17 00:00:00 2001 From: math-and-data Date: Thu, 20 Sep 2018 01:07:45 +0200 Subject: [PATCH 03/16] DOC: Improve the docstring of DataFrame.reindex_like() --- 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 bcf4819d8a0d5..01f4041f44ef7 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -4037,10 +4037,10 @@ def _needs_reindex_multi(self, axes, method, level): def _reindex_multi(self, axes, copy, fill_value): return NotImplemented - _shared_docs['reindex_axis'] = ("""Conform input object to new index with optional - filling logic, placing NA/NaN in locations having no value in the - previous index. A new object is produced unless the new index is - equivalent to the current one and copy=False + _shared_docs['reindex_axis'] = ("""Conform input object to new index + with optional filling logic, placing NA/NaN in locations having + no value in the previous index. A new object is produced unless + the new index is equivalent to the current one and copy=False. Parameters ---------- From 2cbf5ddaa4535eaf430f131f4dc898785f7cc7b0 Mon Sep 17 00:00:00 2001 From: Gjelt Date: Sun, 23 Sep 2018 22:51:14 +0200 Subject: [PATCH 04/16] docstring for .reindex_like --- pandas/core/generic.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 01f4041f44ef7..940edadbe57a8 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3354,13 +3354,11 @@ def reindex_like(self, other, method=None, copy=True, limit=None, Notes ----- - Like calling `.reindex(index=other.index, columns=other.columns, - method=...)` + Like calling `.reindex(index=other.index, columns=other.columns, ...)` Returns ------- - Object - same object type as input, but with changed indices on each axis + Same object type as input, but with changed indices on each axis. See Also -------- From 92c1d2f0e01a0e1ab609bd16c4abad8a79fb9947 Mon Sep 17 00:00:00 2001 From: Gjelt Date: Sun, 23 Sep 2018 23:00:49 +0200 Subject: [PATCH 05/16] docstring .reindex_like: input params details --- pandas/core/generic.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 940edadbe57a8..19b7d7846caf6 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3342,13 +3342,31 @@ def reindex_like(self, other, method=None, copy=True, limit=None, Parameters ---------- other : Object - method : {None, 'backfill'/'bfill', 'pad'/'ffill', 'nearest'} + method : {None, 'backfill'/'bfill', 'pad'/'ffill', 'nearest'}, optional + method to use for filling holes in reindexed DataFrame. + Please note: this is only applicable to DataFrames/Series with a + monotonically increasing/decreasing index. + + * default: don't fill gaps + * pad / ffill: propagate last valid observation forward to next + valid + * backfill / bfill: use next valid observation to fill gap + * nearest: use nearest valid observations to fill gap + copy : boolean, default True + Return a new object, even if the passed indexes are the same limit : int, default None Maximum number of consecutive labels to fill for inexact matches. tolerance : optional - Maximum distance between labels of the other object and this - object for inexact matches. Can be list-like. + Maximum distance between original and new labels for inexact + matches. The values of the index at the matching locations most + satisfy the equation ``abs(index[indexer] - target) <= tolerance``. + + Tolerance may be a scalar value, which applies the same tolerance + to all values, or list-like, which applies variable tolerance per + element. List-like includes list, tuple, array, Series, and must be + the same size as the index and its dtype must exactly match the + index's type. .. versionadded:: 0.21.0 (list-like tolerance) From 5bcee6d11f721b1ea3e8210e7f9f5aaa02b33add Mon Sep 17 00:00:00 2001 From: math-and-data Date: Sun, 23 Sep 2018 23:44:15 +0200 Subject: [PATCH 06/16] docstring .reset_index, .set_index, .reindex, .reindex_like --- pandas/core/frame.py | 50 +++++++++++++++++++------------------ pandas/core/generic.py | 56 ++++++++++++++++++++++-------------------- 2 files changed, 56 insertions(+), 50 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 85150ca9ae47c..dad573155bf45 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -3825,22 +3825,30 @@ def shift(self, periods=1, freq=None, axis=0): def set_index(self, keys, drop=True, append=False, inplace=False, verify_integrity=False): """ + An index is created with existing columns. + Set the DataFrame index (row labels) using one or more existing - columns. By default yields a new object. + columns. The index can replace the existing index or expand on it. Parameters ---------- - keys : column label or list of column labels / arrays + keys : str or list of str or array + Column label or list of column labels / arrays that will form the new index. drop : boolean, default True - Delete columns to be used as the new index + Delete columns to be used as the new index. append : boolean, default False - Whether to append columns to existing index + Whether to append columns to existing index. inplace : boolean, default False - Modify the DataFrame in place (do not create a new object) + Modify the DataFrame in place (do not create a new object). verify_integrity : boolean, default False Check the new index for duplicates. Otherwise defer the check until necessary. Setting to False will improve the performance of this - method + method. + + Returns + ------- + DataFrame + Changed row labels. See Also -------- @@ -3852,22 +3860,17 @@ def set_index(self, keys, drop=True, append=False, inplace=False, -------- >>> df = pd.DataFrame({'month': [1, 4, 7, 10], ... 'year': [2012, 2014, 2013, 2014], - ... 'sale':[55, 40, 84, 31]}) - month sale year - 0 1 55 2012 - 1 4 40 2014 - 2 7 84 2013 - 3 10 31 2014 + ... 'sale': [55, 40, 84, 31]}) Set the index to become the 'month' column: >>> df.set_index('month') - sale year + year sale month - 1 55 2012 - 4 40 2014 - 7 84 2013 - 10 31 2014 + 1 2012 55 + 4 2014 40 + 7 2013 84 + 10 2014 31 Create a multi-index using columns 'year' and 'month': @@ -3888,10 +3891,6 @@ def set_index(self, keys, drop=True, append=False, inplace=False, 2 2014 4 40 3 2013 7 84 4 2014 10 31 - - Returns - ------- - dataframe : DataFrame """ inplace = validate_bool_kwarg(inplace, 'inplace') if not isinstance(keys, list): @@ -3959,6 +3958,8 @@ def set_index(self, keys, drop=True, append=False, inplace=False, def reset_index(self, level=None, drop=False, inplace=False, col_level=0, col_fill=''): """ + An existing index is modified. + For DataFrame with multi-level index, return new DataFrame with labeling information in the columns under the index names, defaulting to 'level_0', 'level_1', etc. if any are None. For a standard index, @@ -3969,12 +3970,12 @@ def reset_index(self, level=None, drop=False, inplace=False, col_level=0, ---------- level : int, str, tuple, or list, default None Only remove the given levels from the index. Removes all levels by - default + default. drop : boolean, default False Do not try to insert index into dataframe columns. This resets the index to the default integer index. inplace : boolean, default False - Modify the DataFrame in place (do not create a new object) + Modify the DataFrame in place (do not create a new object). col_level : int or str, default 0 If the columns have multiple levels, determines which level the labels are inserted into. By default it is inserted into the first @@ -3985,7 +3986,8 @@ def reset_index(self, level=None, drop=False, inplace=False, col_level=0, Returns ------- - resetted : DataFrame + DataFrame + Changed row labels. See Also -------- diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 19b7d7846caf6..22b0da84a62da 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3342,8 +3342,10 @@ def reindex_like(self, other, method=None, copy=True, limit=None, Parameters ---------- other : Object + Its row and column indices are used to define the new indices + of this object. method : {None, 'backfill'/'bfill', 'pad'/'ffill', 'nearest'}, optional - method to use for filling holes in reindexed DataFrame. + Method to use for filling holes in reindexed DataFrame. Please note: this is only applicable to DataFrames/Series with a monotonically increasing/decreasing index. @@ -3354,7 +3356,7 @@ def reindex_like(self, other, method=None, copy=True, limit=None, * nearest: use nearest valid observations to fill gap copy : boolean, default True - Return a new object, even if the passed indexes are the same + Return a new object, even if the passed indexes are the same. limit : int, default None Maximum number of consecutive labels to fill for inexact matches. tolerance : optional @@ -3776,10 +3778,12 @@ def sort_index(self, axis=0, level=None, ascending=True, inplace=False, return self.reindex(**{axis_name: new_axis}) _shared_docs['reindex'] = """ + An index is modified. + Conform %(klass)s to new index with optional filling logic, placing NA/NaN in locations having no value in the previous index. A new object is produced unless the new index is equivalent to the current one and - copy=False + copy=False. Parameters ---------- @@ -3789,7 +3793,7 @@ def sort_index(self, axis=0, level=None, ascending=True, inplace=False, avoid duplicating data %(optional_axis)s method : {None, 'backfill'/'bfill', 'pad'/'ffill', 'nearest'}, optional - method to use for filling holes in reindexed DataFrame. + Method to use for filling holes in reindexed DataFrame. Please note: this is only applicable to DataFrames/Series with a monotonically increasing/decreasing index. @@ -3800,15 +3804,15 @@ def sort_index(self, axis=0, level=None, ascending=True, inplace=False, * nearest: use nearest valid observations to fill gap copy : boolean, default True - Return a new object, even if the passed indexes are the same + Return a new object, even if the passed indexes are the same. level : int or name Broadcast across a level, matching Index values on the - passed MultiIndex level + passed MultiIndex level. fill_value : scalar, default np.NaN Value to use for missing values. Defaults to NaN, but can be any - "compatible" value + "compatible" value. limit : int, default None - Maximum number of consecutive elements to forward or backward fill + Maximum number of consecutive elements to forward or backward fill. tolerance : optional Maximum distance between original and new labels for inexact matches. The values of the index at the matching locations most @@ -3919,12 +3923,12 @@ def sort_index(self, axis=0, level=None, ascending=True, inplace=False, ... index=date_index) >>> df2 prices - 2010-01-01 100 - 2010-01-02 101 + 2010-01-01 100.0 + 2010-01-02 101.0 2010-01-03 NaN - 2010-01-04 100 - 2010-01-05 89 - 2010-01-06 88 + 2010-01-04 100.0 + 2010-01-05 89.0 + 2010-01-06 88.0 Suppose we decide to expand the dataframe to cover a wider date range. @@ -3935,12 +3939,12 @@ def sort_index(self, axis=0, level=None, ascending=True, inplace=False, 2009-12-29 NaN 2009-12-30 NaN 2009-12-31 NaN - 2010-01-01 100 - 2010-01-02 101 + 2010-01-01 100.0 + 2010-01-02 101.0 2010-01-03 NaN - 2010-01-04 100 - 2010-01-05 89 - 2010-01-06 88 + 2010-01-04 100.0 + 2010-01-05 89.0 + 2010-01-06 88.0 2010-01-07 NaN The index entries that did not have a value in the original data frame @@ -3953,15 +3957,15 @@ def sort_index(self, axis=0, level=None, ascending=True, inplace=False, >>> df2.reindex(date_index2, method='bfill') prices - 2009-12-29 100 - 2009-12-30 100 - 2009-12-31 100 - 2010-01-01 100 - 2010-01-02 101 + 2009-12-29 100.0 + 2009-12-30 100.0 + 2009-12-31 100.0 + 2010-01-01 100.0 + 2010-01-02 101.0 2010-01-03 NaN - 2010-01-04 100 - 2010-01-05 89 - 2010-01-06 88 + 2010-01-04 100.0 + 2010-01-05 89.0 + 2010-01-06 88.0 2010-01-07 NaN Please note that the ``NaN`` value present in the original dataframe From 2729193e18796a892aa16f384db3a6aa9cfe5585 Mon Sep 17 00:00:00 2001 From: math-and-data Date: Sun, 23 Sep 2018 23:58:15 +0200 Subject: [PATCH 07/16] docstring set_index: PEP8 style check --- pandas/core/frame.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index dad573155bf45..749dcc8177414 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -3833,7 +3833,8 @@ def set_index(self, keys, drop=True, append=False, inplace=False, Parameters ---------- keys : str or list of str or array - Column label or list of column labels / arrays that will form the new index. + Column label or list of column labels / arrays that will + form the new index. drop : boolean, default True Delete columns to be used as the new index. append : boolean, default False From be1a774c687b7728e3f93b9e11abe35839b4ffb8 Mon Sep 17 00:00:00 2001 From: math-and-data Date: Sun, 4 Nov 2018 19:39:45 +0100 Subject: [PATCH 08/16] reindex: formatting --- pandas/core/frame.py | 30 ++++++++++++++++++------------ pandas/core/generic.py | 16 ++++++++-------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 6faa43fdf3fc1..392e0a4f6d294 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -3919,13 +3919,13 @@ def set_index(self, keys, drop=True, append=False, inplace=False, keys : str or list of str or array Column label or list of column labels / arrays that will form the new index. - drop : boolean, default True + drop : bool, default True Delete columns to be used as the new index. - append : boolean, default False + append : bool, default False Whether to append columns to existing index. - inplace : boolean, default False + inplace : bool, default False Modify the DataFrame in place (do not create a new object). - verify_integrity : boolean, default False + verify_integrity : bool, default False Check the new index for duplicates. Otherwise defer the check until necessary. Setting to False will improve the performance of this method. @@ -3937,15 +3937,21 @@ def set_index(self, keys, drop=True, append=False, inplace=False, See Also -------- - DataFrame.reset_index : opposite of set_index - DataFrame.reindex : change to new indices or expand indices - DataFrame.reindex_like : change to same indices as other DataFrame + DataFrame.reset_index : Opposite of set_index. + DataFrame.reindex : Change to new indices or expand indices. + DataFrame.reindex_like : Change to same indices as other DataFrame. Examples -------- >>> df = pd.DataFrame({'month': [1, 4, 7, 10], ... 'year': [2012, 2014, 2013, 2014], ... 'sale': [55, 40, 84, 31]}) + >>> df + month year sale + 0 1 2012 55 + 1 4 2014 40 + 2 7 2013 84 + 3 10 2014 31 Set the index to become the 'month' column: @@ -4056,10 +4062,10 @@ def reset_index(self, level=None, drop=False, inplace=False, col_level=0, level : int, str, tuple, or list, default None Only remove the given levels from the index. Removes all levels by default. - drop : boolean, default False + drop : bool, default False Do not try to insert index into dataframe columns. This resets the index to the default integer index. - inplace : boolean, default False + inplace : bool, default False Modify the DataFrame in place (do not create a new object). col_level : int or str, default 0 If the columns have multiple levels, determines which level the @@ -4076,9 +4082,9 @@ def reset_index(self, level=None, drop=False, inplace=False, col_level=0, See Also -------- - DataFrame.set_index : opposite of reset_index - DataFrame.reindex : change to new indices or expand indices - DataFrame.reindex_like : change to same indices as other DataFrame + DataFrame.set_index : Opposite of reset_index. + DataFrame.reindex : Change to new indices or expand indices. + DataFrame.reindex_like : Change to same indices as other DataFrame. Examples -------- diff --git a/pandas/core/generic.py b/pandas/core/generic.py index d97dc3547f314..416c39082b4d2 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3326,21 +3326,21 @@ def reindex_like(self, other, method=None, copy=True, limit=None, Parameters ---------- - other : Object + other : Object of the same data type Its row and column indices are used to define the new indices of this object. - method : {None, 'backfill'/'bfill', 'pad'/'ffill', 'nearest'}, optional + method : {None, 'backfill'/'bfill', 'pad'/'ffill', 'nearest'} Method to use for filling holes in reindexed DataFrame. Please note: this is only applicable to DataFrames/Series with a monotonically increasing/decreasing index. - * default: don't fill gaps + * None (default): don't fill gaps * pad / ffill: propagate last valid observation forward to next valid * backfill / bfill: use next valid observation to fill gap * nearest: use nearest valid observations to fill gap - copy : boolean, default True + copy : bool, default True Return a new object, even if the passed indexes are the same. limit : int, default None Maximum number of consecutive labels to fill for inexact matches. @@ -3359,7 +3359,7 @@ def reindex_like(self, other, method=None, copy=True, limit=None, Notes ----- - Like calling `.reindex(index=other.index, columns=other.columns, ...)` + Like calling `.reindex(index=other.index, columns=other.columns,...)`. Returns ------- @@ -3367,9 +3367,9 @@ def reindex_like(self, other, method=None, copy=True, limit=None, See Also -------- - DataFrame.set_index : set row labels - DataFrame.reset_index : remove row labels or move them to new columns - DataFrame.reindex : change to new indices or expand indices + DataFrame.set_index : Set row labels. + DataFrame.reset_index : Remove row labels or move them to new columns. + DataFrame.reindex : Change to new indices or expand indices. Examples -------- From 7072e4aeeda578b10c3f25b394a0b3e30912d528 Mon Sep 17 00:00:00 2001 From: math-and-data Date: Sun, 4 Nov 2018 19:42:12 +0100 Subject: [PATCH 09/16] reindex: formatting of docstring --- pandas/core/generic.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 416c39082b4d2..3c51f0ef74078 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3769,18 +3769,18 @@ def reindex(self, *args, **kwargs): New labels / index to conform to. Preferably an Index object to avoid duplicating data %(optional_axis)s - method : {None, 'backfill'/'bfill', 'pad'/'ffill', 'nearest'}, optional + method : {None, 'backfill'/'bfill', 'pad'/'ffill', 'nearest'} Method to use for filling holes in reindexed DataFrame. Please note: this is only applicable to DataFrames/Series with a monotonically increasing/decreasing index. - * default: don't fill gaps + * None (default): don't fill gaps * pad / ffill: propagate last valid observation forward to next valid * backfill / bfill: use next valid observation to fill gap * nearest: use nearest valid observations to fill gap - copy : boolean, default True + copy : bool, default True Return a new object, even if the passed indexes are the same. level : int or name Broadcast across a level, matching Index values on the @@ -3805,9 +3805,9 @@ def reindex(self, *args, **kwargs): See Also -------- - DataFrame.set_index : set row labels - DataFrame.reset_index : remove row labels or move them to new columns - DataFrame.reindex_like : change to same indices as other DataFrame + DataFrame.set_index : Set row labels. + DataFrame.reset_index : Remove row labels or move them to new columns. + DataFrame.reindex_like : Change to same indices as other DataFrame. Examples -------- From 88e6e375ccb0acffd10eed14d897b33c3b1eb59b Mon Sep 17 00:00:00 2001 From: Marc Garcia Date: Sun, 4 Nov 2018 21:09:18 +0100 Subject: [PATCH 10/16] Update pandas/core/generic.py Co-Authored-By: math-and-data --- 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 3c51f0ef74078..4276ac316ce46 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3359,7 +3359,7 @@ def reindex_like(self, other, method=None, copy=True, limit=None, Notes ----- - Like calling `.reindex(index=other.index, columns=other.columns,...)`. + Like calling ``.reindex(index=other.index, columns=other.columns,...)``. Returns ------- From 38c5c94dfc6b28c533e736ddc6befac3cb1f5545 Mon Sep 17 00:00:00 2001 From: Marc Garcia Date: Sun, 4 Nov 2018 21:21:18 +0100 Subject: [PATCH 11/16] Update pandas/core/generic.py Co-Authored-By: math-and-data --- 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 4276ac316ce46..3550523ad993d 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3760,7 +3760,7 @@ def reindex(self, *args, **kwargs): Conform %(klass)s to new index with optional filling logic, placing NA/NaN in locations having no value in the previous index. A new object is produced unless the new index is equivalent to the current one and - copy=False. + ``copy=False``. Parameters ---------- From 45187914b625358f5e4161f01addeb6753f212a6 Mon Sep 17 00:00:00 2001 From: math-and-data Date: Sun, 4 Nov 2018 21:24:26 +0100 Subject: [PATCH 12/16] reindex_like print example df --- pandas/core/generic.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 3c51f0ef74078..02ac8e0c95fba 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3363,7 +3363,8 @@ def reindex_like(self, other, method=None, copy=True, limit=None, Returns ------- - Same object type as input, but with changed indices on each axis. + Object + Same data type as input, but with changed indices on each axis. See Also -------- @@ -3381,6 +3382,13 @@ def reindex_like(self, other, method=None, copy=True, limit=None, ... index=pd.date_range(start='2014-02-12', ... end='2014-02-15', freq='D')) + >>> df_weather_station_1 + temp_celsius temp_fahrenheit windspeed + 2014-02-12 24.3 75.7 high + 2014-02-13 31.0 87.8 high + 2014-02-14 22.0 71.6 medium + 2014-02-15 35.0 95.0 medium + >>> df_weather_station_2 = pd.DataFrame([[28, 'low'], ... [30, 'low'], ... [35.1, 'medium']], @@ -3388,6 +3396,12 @@ def reindex_like(self, other, method=None, copy=True, limit=None, ... index=pd.DatetimeIndex(['2014-02-12', '2014-02-13', ... '2014-02-15'])) + >>> df_weather_station_2 + temp_celsius windspeed + 2014-02-12 28.0 low + 2014-02-13 30.0 low + 2014-02-15 35.1 medium + >>> df_weather_station_2.reindex_like(df_weather_station_1) temp_celsius temp_fahrenheit windspeed 2014-02-12 28.0 NaN low From 363e8c0657ca8df42d22056a50dd469e67d51e26 Mon Sep 17 00:00:00 2001 From: math-and-data Date: Sun, 4 Nov 2018 22:00:26 +0100 Subject: [PATCH 13/16] reindex_like params --- 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 9051a12fc2399..3fba10628349b 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3779,8 +3779,8 @@ def reindex(self, *args, **kwargs): Parameters ---------- %(optional_labels)s - %(axes)s : array-like, optional (should be specified using keywords) - New labels / index to conform to. Preferably an Index object to + %(axes)s : array-like, optional + New labels / index to conform to, should be specified using keywords. Preferably an Index object to avoid duplicating data %(optional_axis)s method : {None, 'backfill'/'bfill', 'pad'/'ffill', 'nearest'} From 4d5984450e5c9915bb70ba152f3e400f20548048 Mon Sep 17 00:00:00 2001 From: math-and-data Date: Sun, 4 Nov 2018 22:16:17 +0100 Subject: [PATCH 14/16] reindex docstring: pep8 line length --- 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 3fba10628349b..01b0a190a2627 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3359,12 +3359,11 @@ def reindex_like(self, other, method=None, copy=True, limit=None, Notes ----- - Like calling ``.reindex(index=other.index, columns=other.columns,...)``. + Like calling ``.reindex(index=other.index,columns=other.columns,...)``. Returns ------- - Object - Same data type as input, but with changed indices on each axis. + Object with input data type, but with changed indices on each axis. See Also -------- @@ -3780,8 +3779,8 @@ def reindex(self, *args, **kwargs): ---------- %(optional_labels)s %(axes)s : array-like, optional - New labels / index to conform to, should be specified using keywords. Preferably an Index object to - avoid duplicating data + New labels / index to conform to, should be specified using + keywords. Preferably an Index object to avoid duplicating data %(optional_axis)s method : {None, 'backfill'/'bfill', 'pad'/'ffill', 'nearest'} Method to use for filling holes in reindexed DataFrame. @@ -3970,7 +3969,7 @@ def reindex(self, *args, **kwargs): Returns ------- - reindexed : %(klass)s + %(klass)s with changed index. """ # TODO: Decide if we care about having different examples for different # kinds From 6bf4977335b62b10e256286d23fd224e461fe497 Mon Sep 17 00:00:00 2001 From: Marc Garcia Date: Wed, 21 Nov 2018 18:36:05 +0000 Subject: [PATCH 15/16] Addressing code review comments --- pandas/core/frame.py | 25 +++++++++++-------------- pandas/core/generic.py | 13 +++++++------ 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 392e0a4f6d294..176c35b7e6128 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -3909,16 +3909,15 @@ def shift(self, periods=1, freq=None, axis=0): def set_index(self, keys, drop=True, append=False, inplace=False, verify_integrity=False): """ - An index is created with existing columns. + Set the DataFrame index using existing columns. Set the DataFrame index (row labels) using one or more existing columns. The index can replace the existing index or expand on it. Parameters ---------- - keys : str or list of str or array - Column label or list of column labels / arrays that will - form the new index. + keys : label or list of label + Name or names of the columns that will be used as the index. drop : bool, default True Delete columns to be used as the new index. append : bool, default False @@ -4049,13 +4048,11 @@ def set_index(self, keys, drop=True, append=False, inplace=False, def reset_index(self, level=None, drop=False, inplace=False, col_level=0, col_fill=''): """ - An existing index is modified. + Reset the index, or a level of it. - For DataFrame with multi-level index, return new DataFrame with - labeling information in the columns under the index names, defaulting - to 'level_0', 'level_1', etc. if any are None. For a standard index, - the index name will be used (if set), otherwise a default 'index' or - 'level_0' (if 'index' is already taken) will be used. + Reset the index of the DataFrame, and use the default one instead. + If the DataFrame has a MultiIndex, this method can remove one or more + of them. Parameters ---------- @@ -4078,7 +4075,7 @@ def reset_index(self, level=None, drop=False, inplace=False, col_level=0, Returns ------- DataFrame - Changed row labels. + DataFrame with the new index. See Also -------- @@ -4088,9 +4085,9 @@ def reset_index(self, level=None, drop=False, inplace=False, col_level=0, Examples -------- - >>> df = pd.DataFrame([('bird', 389.0), - ... ('bird', 24.0), - ... ('mammal', 80.5), + >>> df = pd.DataFrame([('bird', 389.0), + ... ('bird', 24.0), + ... ('mammal', 80.5), ... ('mammal', np.nan)], ... index=['falcon', 'parrot', 'lion', 'monkey'], ... columns=('class', 'max_speed')) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 01b0a190a2627..8040d16d2fe96 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3359,7 +3359,8 @@ def reindex_like(self, other, method=None, copy=True, limit=None, Notes ----- - Like calling ``.reindex(index=other.index,columns=other.columns,...)``. + Same as calling + ``.reindex(index=other.index, columns=other.columns,...)``. Returns ------- @@ -4083,14 +4084,14 @@ def _reindex_multi(self, axes, copy, fill_value): See Also -------- - DataFrame.set_index : set row labels - DataFrame.reset_index : remove row labels or move them to new columns - DataFrame.reindex : change to new indices or expand indices - DataFrame.reindex_like : change to same indices as other DataFrame + DataFrame.set_index : Set row labels. + DataFrame.reset_index : Remove row labels or move them to new columns. + DataFrame.reindex : Change to new indices or expand indices. + DataFrame.reindex_like : Change to same indices as other DataFrame. Returns ------- - reindexed : %(klass)s + %(klass)s Examples -------- From 7df1f792da8c724ef62ad51f8e8164ed92176029 Mon Sep 17 00:00:00 2001 From: Marc Garcia Date: Mon, 26 Nov 2018 15:56:31 +0000 Subject: [PATCH 16/16] Addressing review comments --- pandas/core/frame.py | 2 +- pandas/core/generic.py | 49 +++++++++++++++++++++--------------------- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 3b74fb7ad0cc9..4a8d40893c9a3 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -4093,7 +4093,7 @@ def reset_index(self, level=None, drop=False, inplace=False, col_level=0, Reset the index of the DataFrame, and use the default one instead. If the DataFrame has a MultiIndex, this method can remove one or more - of them. + levels. Parameters ---------- diff --git a/pandas/core/generic.py b/pandas/core/generic.py index b963e9d9f7d6b..c6c9589bdc059 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -3438,7 +3438,7 @@ def reindex_like(self, other, method=None, copy=True, limit=None, Return an object with matching indices as other object. Conform the object to the same index on all axes. Optional - filling logic, placing NA/NaN in locations having no value + filling logic, placing NaN in locations having no value in the previous index. A new object is produced unless the new index is equivalent to the current one and copy=False. @@ -3475,14 +3475,10 @@ def reindex_like(self, other, method=None, copy=True, limit=None, .. versionadded:: 0.21.0 (list-like tolerance) - Notes - ----- - Same as calling - ``.reindex(index=other.index, columns=other.columns,...)``. - Returns ------- - Object with input data type, but with changed indices on each axis. + Series or DataFrame + Same type as caller, but with changed indices on each axis. See Also -------- @@ -3490,37 +3486,42 @@ def reindex_like(self, other, method=None, copy=True, limit=None, DataFrame.reset_index : Remove row labels or move them to new columns. DataFrame.reindex : Change to new indices or expand indices. + Notes + ----- + Same as calling + ``.reindex(index=other.index, columns=other.columns,...)``. + Examples -------- - >>> df_weather_station_1 = pd.DataFrame([[24.3, 75.7, 'high'], - ... [31, 87.8, 'high'], - ... [22, 71.6, 'medium'], - ... [35, 95, 'medium']], - ... columns=['temp_celsius', 'temp_fahrenheit', 'windspeed'], - ... index=pd.date_range(start='2014-02-12', - ... end='2014-02-15', freq='D')) - - >>> df_weather_station_1 + >>> df1 = pd.DataFrame([[24.3, 75.7, 'high'], + ... [31, 87.8, 'high'], + ... [22, 71.6, 'medium'], + ... [35, 95, 'medium']], + ... columns=['temp_celsius', 'temp_fahrenheit', 'windspeed'], + ... index=pd.date_range(start='2014-02-12', + ... end='2014-02-15', freq='D')) + + >>> df1 temp_celsius temp_fahrenheit windspeed 2014-02-12 24.3 75.7 high 2014-02-13 31.0 87.8 high 2014-02-14 22.0 71.6 medium 2014-02-15 35.0 95.0 medium - >>> df_weather_station_2 = pd.DataFrame([[28, 'low'], - ... [30, 'low'], - ... [35.1, 'medium']], - ... columns=['temp_celsius', 'windspeed'], - ... index=pd.DatetimeIndex(['2014-02-12', '2014-02-13', - ... '2014-02-15'])) + >>> df2 = pd.DataFrame([[28, 'low'], + ... [30, 'low'], + ... [35.1, 'medium']], + ... columns=['temp_celsius', 'windspeed'], + ... index=pd.DatetimeIndex(['2014-02-12', '2014-02-13', + ... '2014-02-15'])) - >>> df_weather_station_2 + >>> df2 temp_celsius windspeed 2014-02-12 28.0 low 2014-02-13 30.0 low 2014-02-15 35.1 medium - >>> df_weather_station_2.reindex_like(df_weather_station_1) + >>> df2.reindex_like(df1) temp_celsius temp_fahrenheit windspeed 2014-02-12 28.0 NaN low 2014-02-13 30.0 NaN low