cut_before
- trashpanda.cut_before(source_to_cut: Union[pandas.core.series.Series, pandas.core.frame.DataFrame], cutting_index: float) Union[pandas.core.series.Series, pandas.core.frame.DataFrame]
Cuts a dataframe dropping the part after the cutting index. The cutting index will be added to the frame, which values are being interpolated, if inside.
- Parameters
source_to_cut (Union[Series, DataFrame]) – Source frame to be cut at the cutting index.
cutting_index (float) – Cutting index at which the source frame should be cut.
- Returns
Union[Series, DataFrame]
Examples
Usage with pandas.Series
>>> import numpy >>> from pandas import Series, Index >>> from doctestprinter import doctest_print >>> from trashpanda import cut_after >>> sample_series = Series( ... numpy.arange(3.0), ... index=Index([0.1, 0.2, 0.3], name="x"), ... name="y" ... ) >>> sample_series x 0.1 0.0 0.2 1.0 0.3 2.0 Name: y, dtype: float64 >>> cut_before(sample_series, 0.3) x 0.3 2.0 Name: y, dtype: float64 >>> cut_before(sample_series, 0.14) x 0.14 0.4 0.20 1.0 0.30 2.0 Name: y, dtype: float64 >>> cut_before(sample_series, 0.2) x 0.2 1.0 0.3 2.0 Name: y, dtype: float64 >>> cut_before(sample_series, 0.09) x 0.09 NaN 0.10 0.0 0.20 1.0 0.30 2.0 Name: y, dtype: float64 >>> cut_before(sample_series, 0.31) Series([], Name: y, dtype: float64)
Usage with a pandas.DataFrame
>>> import numpy >>> from pandas import DataFrame, Index >>> from doctestprinter import doctest_print >>> from trashpanda import cut_before >>> sample_frame = DataFrame( ... numpy.arange(6.0).reshape(3, 2), ... columns=["b", "a"], ... index=Index([0.1, 0.2, 0.3], name="x") ... ) >>> doctest_print(sample_frame) b a x 0.1 0.0 1.0 0.2 2.0 3.0 0.3 4.0 5.0 >>> doctest_print(cut_before(sample_frame, 0.3)) b a x 0.3 4.0 5.0 >>> doctest_print(cut_before(sample_frame, 0.14)) b a x 0.14 0.8 1.8 0.20 2.0 3.0 0.30 4.0 5.0 >>> doctest_print(cut_before(sample_frame, 0.2)) b a x 0.2 2.0 3.0 0.3 4.0 5.0 >>> doctest_print(cut_before(sample_frame, 0.09)) b a x 0.09 NaN NaN 0.10 0.0 1.0 0.20 2.0 3.0 0.30 4.0 5.0 >>> cut_before(sample_frame, 0.31) Empty DataFrame Columns: [b, a] Index: []