dtoolkit.accessor.dataframe.drop_inf#

dtoolkit.accessor.dataframe.drop_inf(df: DataFrame, /, axis: Literal[0, 1, 'index', 'columns'] = 0, how: Literal['any', 'all'] = 'any', inf: Literal['all', 'pos', 'neg'] = 'all', subset: list[str] = None) DataFrame[source]#

Remove inf values.

Parameters:
axis{0 or ‘index’, 1 or ‘columns’}, default 0

Determine if rows or columns which contain inf values are removed.

  • 0, or ‘index’ : Drop rows which contain inf values.

  • 1, or ‘columns’ : Drop columns which contain inf value.

how{‘any’, ‘all’}, default ‘any’

Determine if row or column is removed from DataFrame, when we have at least one inf or all inf.

  • ‘any’ : If any inf values are present, drop that row or column.

  • ‘all’ : If all values are inf, drop that row or column.

inf{‘all’, ‘pos’, ‘+’, ‘neg’, ‘-‘}, default ‘all’
  • ‘all’ : Remove inf and -inf.

  • ‘pos’ / ‘+’ : Only remove inf.

  • ‘neg’ / ‘-’ : Only remove -inf.

subsetarray-like, optional

Labels along other axis to consider, e.g. if you are dropping rows these would be a list of columns to include.

Returns:
DataFrame

DataFrame with inf entries dropped from it.

See also

dtoolkit.accessor.series.drop_inf

Series drops inf values.

Examples

>>> import dtoolkit
>>> import pandas as pd
>>> import numpy as np
>>> df = pd.DataFrame(
...     {
...         "name": ['Alfred', 'Batman', 'Catwoman'],
...         "toy": [np.inf, 'Batmobile', 'Bullwhip'],
...         "born": [np.inf, pd.Timestamp("1940-04-25"), -np.inf],
...     },
... )
>>> df
       name        toy                 born
0    Alfred        inf                  inf
1    Batman  Batmobile  1940-04-25 00:00:00
2  Catwoman   Bullwhip                 -inf

Drop the rows where at least one element is inf and -inf.

>>> df.drop_inf()
     name        toy                 born
1  Batman  Batmobile  1940-04-25 00:00:00

Drop the columns where at least one element is inf and -inf.

>>> df.drop_inf(axis='columns')
       name
0    Alfred
1    Batman
2  Catwoman

Drop the rows where all elements are inf and -inf.

>>> df.drop_inf(how='all')
       name        toy                 born
0    Alfred        inf                  inf
1    Batman  Batmobile  1940-04-25 00:00:00
2  Catwoman   Bullwhip                 -inf

Drop the rows where at least one element is -inf.

>>> df.drop_inf(inf='neg')
       name        toy                 born
0    Alfred        inf                  inf
1    Batman  Batmobile  1940-04-25 00:00:00

Define in which columns to look for inf and -inf values.

>>> df.drop_inf(subset=['name', 'toy'])
       name        toy                 born
1    Batman  Batmobile  1940-04-25 00:00:00
2  Catwoman   Bullwhip                 -inf

Keep the DataFrame with valid entries in the same variable.