- API reference
- Series
- pandas.Serie...
pandas.Series.reindex_like#
- Series.reindex_like(other,method=None,copy=None,limit=None,tolerance=None)[source]#
Return an object with matching indices as other object.
Conform the object to the same index on all axes. Optionalfilling logic, placing NaN in locations having no valuein the previous index. A new object is produced unless thenew index is equivalent to the current one and copy=False.
- Parameters:
- otherObject of the same data type
Its row and column indices are used to define the new indicesof this object.
- 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 amonotonically increasing/decreasing index.
None (default): don’t fill gaps
pad / ffill: propagate last valid observation forward to nextvalid
backfill / bfill: use next valid observation to fill gap
nearest: use nearest valid observations to fill gap.
- copybool, default True
Return a new object, even if the passed indexes are the same.
Note
Thecopy keyword will change behavior in pandas 3.0.Copy-on-Writewill be enabled by default, which means that all methods with acopy keyword will use a lazy copy mechanism to defer the copy andignore thecopy keyword. Thecopy keyword will be removed in afuture version of pandas.
You can already get the future behavior and improvements throughenabling copy on write
pd.options.mode.copy_on_write=True
- limitint, default None
Maximum number of consecutive labels to fill for inexact matches.
- toleranceoptional
Maximum distance between original and new labels for inexactmatches. The values of the index at the matching locations mustsatisfy the equation
abs(index[indexer]-target)<=tolerance
.Tolerance may be a scalar value, which applies the same toleranceto all values, or list-like, which applies variable tolerance perelement. List-like includes list, tuple, array, Series, and must bethe same size as the index and its dtype must exactly match theindex’s type.
- Returns:
- Series or DataFrame
Same type as caller, 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.
Notes
Same as calling
.reindex(index=other.index,columns=other.columns,...)
.Examples
>>>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 windspeed2014-02-12 24.3 75.7 high2014-02-13 31.0 87.8 high2014-02-14 22.0 71.6 medium2014-02-15 35.0 95.0 medium
>>>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']))
>>>df2 temp_celsius windspeed2014-02-12 28.0 low2014-02-13 30.0 low2014-02-15 35.1 medium
>>>df2.reindex_like(df1) temp_celsius temp_fahrenheit windspeed2014-02-12 28.0 NaN low2014-02-13 30.0 NaN low2014-02-14 NaN NaN NaN2014-02-15 35.1 NaN medium