portfolios.filter_sorting_data()

Filter sorting data.

Usage

Source

portfolios.filter_sorting_data(
    data, filter_options=None, data_options=None, quiet=False
)

Applies sample construction filters to a data frame before portfolio sorting. Filters are applied in a fixed order: financials exclusion, utilities exclusion, minimum stock price, minimum size quantile, minimum listing age, positive book equity, and positive earnings. An informational warning is emitted for each filter that actually removes at least one observation.

Parameters

data: pl.DataFrame

Data frame containing the stock-level panel data to be filtered.

filter_options: dict = None

Dict produced by ‘filter_options’. If None (the default), the defaults from ‘filter_options’ are used (i.e., no filters are applied). The accepted entries include:

  • ‘exclude_financials’ (bool): Whether to exclude financial firms (SIC codes 6000 to 6799). Defaults to False.
  • ‘exclude_utilities’ (bool): Whether to exclude utility firms (SIC codes 4900 to 4999). Defaults to False.
  • ‘min_stock_price’ (float, optional): Minimum stock price required to include an observation. None (the default) applies no price filter.
  • ‘min_size_quantile’ (float, optional): Minimum cross-sectional size quantile (based on lagged market cap) required to include an observation. None (the default) applies no size quantile filter. The cutoff is computed from NYSE stocks only; the ‘exchange’ column (mapped via ‘data_options’) must be present or a ValueError is raised.
  • ‘min_listing_age’ (float, optional): Minimum number of months a stock must have been listed in CRSP. None (the default) applies no listing age filter.
  • ‘exclude_negative_book_equity’ (bool): Whether to exclude observations with non-positive book equity. Defaults to False.
  • ‘exclude_negative_earnings’ (bool): Whether to exclude observations with non-positive earnings. Defaults to False.
data_options: dict = None

Column-name mapping (see ‘data_options’). The ‘siccd’ element specifies the SIC code column, ‘price’ specifies the (adjusted) price column, ‘mktcap_lag’ specifies the market capitalization column, ‘date’ specifies the date column, ‘listing_age’ specifies the listing age column, ‘be’ specifies the book equity column, and ‘earnings’ specifies the earnings column. Uses the ‘data_options’ defaults when None.

quiet: bool = False
Whether informational messages should be suppressed.

Returns

pl.DataFrame
Filtered data frame, preserving the class and structure of the input.

Examples

import datetime as dt
import polars as pl
from tidyfinance import filter_sorting_data, filter_options
data = pl.DataFrame({
    'permno': range(1, 6),
    'date': [dt.date(2020, 1, 1)] * 5,
    'siccd': [6100, 2000, 4950, 3000, 6500],
    'prc_adj': [5.0, 0.5, 15.0, 20.0, 10.0],
})
filter_sorting_data(
    data,
    filter_options=filter_options(
        exclude_financials=True,
        min_stock_price=1,
    ),
)