janitor.fill_direction

janitor.fill_direction(df: pandas.core.frame.DataFrame, directions: Dict[Hashable, str] = None, limit: Optional[int] = None) → pandas.core.frame.DataFrame[source]

Provide a method-chainable function for filling missing values in selected columns.

Missing values are filled using the next or previous entry. The columns are paired with the directions in a dictionary. It is a wrapper for pd.Series.ffill and pd.Series.bfill.

import pandas as pd
import numpy as np
import janitor as jn

df = pd.DataFrame({"text": ["ragnar", np.nan, "sammywemmy",
                            np.nan, "ginger"],
                   "code" : [np.nan, 2, 3, np.nan, 5]})

df

   text          code
0 ragnar         NaN
1 NaN            2.0
2 sammywemmy     3.0
3 NaN            NaN
4 ginger         5.0

Fill on a single column:

df.fill_direction({"text" : "up"})

   text          code
0 ragnar         NaN
1 sammywemmy     2.0
2 sammywemmy     3.0
3 ginger         NaN
4 ginger         5.0

Fill on multiple columns:

df.fill_direction({"text" : "down", "code" : "down"})

   text          code
0 ragnar         NaN
1 ragnar         2.0
2 sammywemmy     3.0
3 sammywemmy     3.0
4 ginger         5.0

Fill multiple columns in different directions:

df.fill_direction({"text" : "up", "code" : "down"})

   text          code
0 ragnar         NaN
1 sammywemmy     2.0
2 sammywemmy     3.0
3 ginger         3.0
4 ginger         5.0

Functional usage syntax:

import pandas as pd
import janitor as jn

df = pd.DataFrame(...)
df = jn.fill_direction(
    df = df,
    directions = {column_1 : direction_1,
                  column_2 : direction_2,
                  ...},
    limit = None # limit must be None or greater than 0
    )

Method-chaining usage syntax:

import pandas as pd
import janitor as jn

df = (
    pd.DataFrame(...)
    .fill_direction(
    directions = {column_1 : direction_1,
                  column_2 : direction_2,
                  ...},
    limit = None # limit must be None or greater than 0
    )
)
Parameters
  • df – A pandas dataframe.

  • directions – Key - value pairs of columns and directions. Directions can be either down (default), up, updown (fill up then down) and downup (fill down then up).

  • limit – number of consecutive null values to forward/backward fill. Value must None or greater than 0.

Returns

A pandas dataframe with modified column(s).

Raises
  • ValueError – if column supplied is not in the dataframe.

  • ValueError – if direction supplied is not one of down, up, updown, or downup.