Skip to main content
Version: Next

ts_shape.features.stats.string_stats

Classes
NameDescription
StringStatisticsProvides class methods to calculate statistics on string columns in a pandas DataFrame.

StringStatistics​

classsource

Bases: Base

Provides class methods to calculate statistics on string columns in a pandas DataFrame.

Functions
NameDescription
count_uniqueReturns the number of unique strings in the column.
most_frequentReturns the most frequent string in the column.
count_most_frequentReturns the count of the most frequent string in the column.
count_nullReturns the number of null (NaN) values in the column.
average_string_lengthReturns the average length of strings in the column, excluding null values.
longest_stringReturns the longest string in the column.
shortest_stringReturns the shortest string in the column.
string_length_summaryReturns a summary of string lengths, including min, max, and average lengths.
most_common_n_stringsReturns the top N most frequent strings in the column.
contains_substring_countCounts how many strings contain the specified substring.
starts_with_countCounts how many strings start with the specified prefix.
ends_with_countCounts how many strings end with the specified suffix.
uppercase_percentageReturns the percentage of strings that are fully uppercase.
lowercase_percentageReturns the percentage of strings that are fully lowercase.
contains_digit_countCounts how many strings contain digits.
summary_as_dictReturns a dictionary with comprehensive string statistics for the specified column.
summary_as_dataframeReturns a DataFrame with comprehensive string statistics for the specified column.

count_unique​

classmethodsource
count_unique(
dataframe: pd.DataFrame, value_column: str = "value_string"
) -> int

Returns the number of unique strings in the column.

Parameters
NameTypeDescriptionDefault
dataframeDataFrameInput DataFrame.required
value_columnstrName of the string column to analyze. Default "value_string".'value_string'
Returns
TypeDescription
intNumber of distinct non-null values.

most_frequent​

classmethodsource
most_frequent(
dataframe: pd.DataFrame, value_column: str = "value_string"
) -> str | None

Returns the most frequent string in the column.

Parameters
NameTypeDescriptionDefault
dataframeDataFrameInput DataFrame.required
value_columnstrName of the string column to analyze. Default "value_string".'value_string'
Returns
TypeDescription
str | NoneThe most frequent non-null string (the smallest on ties), or
str | NoneNone when the column holds no non-null value.

count_most_frequent​

classmethodsource
count_most_frequent(
dataframe: pd.DataFrame, value_column: str = "value_string"
) -> int

Returns the count of the most frequent string in the column.

Parameters
NameTypeDescriptionDefault
dataframeDataFrameInput DataFrame.required
value_columnstrName of the string column to analyze. Default "value_string".'value_string'
Returns
TypeDescription
intOccurrences of :meth:most_frequent; 0 when the column holds
intno non-null value (it raised IndexError before).

count_null​

classmethodsource
count_null(dataframe: pd.DataFrame, value_column: str = 'value_string') -> int

Returns the number of null (NaN) values in the column.

Parameters
NameTypeDescriptionDefault
dataframeDataFrameInput DataFrame.required
value_columnstrName of the string column to analyze. Default "value_string".'value_string'
Returns
TypeDescription
intNumber of missing values.

average_string_length​

classmethodsource
average_string_length(
dataframe: pd.DataFrame, value_column: str = "value_string"
) -> float

Returns the average length of strings in the column, excluding null values.

Parameters
NameTypeDescriptionDefault
dataframeDataFrameInput DataFrame.required
value_columnstrName of the string column to analyze. Default "value_string".'value_string'
Returns
TypeDescription
floatMean length of the non-null strings; NaN when there is none.

longest_string​

classmethodsource
longest_string(
dataframe: pd.DataFrame, value_column: str = "value_string"
) -> str | None

Returns the longest string in the column.

Parameters
NameTypeDescriptionDefault
dataframeDataFrameInput DataFrame.required
value_columnstrName of the string column to analyze. Default "value_string".'value_string'
Returns
TypeDescription
str | NoneThe longest non-null string, or None when the column holds no
str | Nonenon-null value.

shortest_string​

classmethodsource
shortest_string(
dataframe: pd.DataFrame, value_column: str = "value_string"
) -> str | None

Returns the shortest string in the column.

Parameters
NameTypeDescriptionDefault
dataframeDataFrameInput DataFrame.required
value_columnstrName of the string column to analyze. Default "value_string".'value_string'
Returns
TypeDescription
str | NoneThe shortest non-null string, or None when the column holds no
str | Nonenon-null value.

string_length_summary​

classmethodsource
string_length_summary(
dataframe: pd.DataFrame, value_column: str = "value_string"
) -> pd.DataFrame

Returns a summary of string lengths, including min, max, and average lengths.

Parameters
NameTypeDescriptionDefault
dataframeDataFrameInput DataFrame.required
value_columnstrName of the string column to analyze. Default "value_string".'value_string'
Returns
TypeDescription
DataFrameOne-row DataFrame with columns Min Length, Max Length and Average Length (null values excluded).

most_common_n_strings​

classmethodsource
most_common_n_strings(
dataframe: pd.DataFrame, n: int, value_column: str = "value_string"
) -> pd.Series

Returns the top N most frequent strings in the column.

Parameters
NameTypeDescriptionDefault
dataframeDataFrameInput DataFrame.required
nintNumber of strings to return.required
value_columnstrName of the string column to analyze. Default "value_string".'value_string'
Returns
TypeDescription
SeriesSeries of counts indexed by string, most frequent first.

contains_substring_count​

classmethodsource
contains_substring_count(
dataframe: pd.DataFrame,
substring: str,
value_column: str = "value_string",
*,
regex: bool = False
) -> int

Counts how many strings contain the specified substring.

Parameters
NameTypeDescriptionDefault
dataframeDataFrameInput DataFrame.required
substringstrText to look for, matched literally by default, so characters such as ., ( or + have no special meaning.required
value_columnstrName of the string column to analyze. Default "value_string".'value_string'
regexboolIf False (default), match substring literally; if True, treat it as a regular expression.False
Returns
TypeDescription
intNumber of non-null strings that contain substring.

starts_with_count​

classmethodsource
starts_with_count(
dataframe: pd.DataFrame, prefix: str, value_column: str = "value_string"
) -> int

Counts how many strings start with the specified prefix.

Parameters
NameTypeDescriptionDefault
dataframeDataFrameInput DataFrame.required
prefixstrPrefix to match.required
value_columnstrName of the string column to analyze. Default "value_string".'value_string'
Returns
TypeDescription
intNumber of non-null strings that start with prefix.

ends_with_count​

classmethodsource
ends_with_count(
dataframe: pd.DataFrame, suffix: str, value_column: str = "value_string"
) -> int

Counts how many strings end with the specified suffix.

Parameters
NameTypeDescriptionDefault
dataframeDataFrameInput DataFrame.required
suffixstrSuffix to match.required
value_columnstrName of the string column to analyze. Default "value_string".'value_string'
Returns
TypeDescription
intNumber of non-null strings that end with suffix.

uppercase_percentage​

classmethodsource
uppercase_percentage(
dataframe: pd.DataFrame, value_column: str = "value_string"
) -> float

Returns the percentage of strings that are fully uppercase.

Parameters
NameTypeDescriptionDefault
dataframeDataFrameInput DataFrame.required
value_columnstrName of the string column to analyze. Default "value_string".'value_string'
Returns
TypeDescription
floatPercentage (0-100) of the non-null strings that are fully uppercase; NaN when the column holds no non-null value.

lowercase_percentage​

classmethodsource
lowercase_percentage(
dataframe: pd.DataFrame, value_column: str = "value_string"
) -> float

Returns the percentage of strings that are fully lowercase.

Parameters
NameTypeDescriptionDefault
dataframeDataFrameInput DataFrame.required
value_columnstrName of the string column to analyze. Default "value_string".'value_string'
Returns
TypeDescription
floatPercentage (0-100) of the non-null strings that are fully lowercase; NaN when the column holds no non-null value.

contains_digit_count​

classmethodsource
contains_digit_count(
dataframe: pd.DataFrame, value_column: str = "value_string"
) -> int

Counts how many strings contain digits.

Parameters
NameTypeDescriptionDefault
dataframeDataFrameInput DataFrame.required
value_columnstrName of the string column to analyze. Default "value_string".'value_string'
Returns
TypeDescription
intNumber of non-null strings with at least one digit.

summary_as_dict​

classmethodsource
summary_as_dict(
dataframe: pd.DataFrame, value_column: str
) -> dict[str, int | str | float | None]

Returns a dictionary with comprehensive string statistics for the specified column.

Parameters
NameTypeDescriptionDefault
dataframeDataFrameInput DataFrame.required
value_columnstrName of the string column to analyze.required
Returns
TypeDescription
dict[str, int | str | float | None]Dict with keys unique_values, most_frequent, count_most_frequent, count_null, average_string_length, longest_string, shortest_string, uppercase_percentage, lowercase_percentage, contains_digit_count, least_common and frequency_least_common.

summary_as_dataframe​

classmethodsource
summary_as_dataframe(
dataframe: pd.DataFrame, value_column: str
) -> pd.DataFrame

Returns a DataFrame with comprehensive string statistics for the specified column.

Parameters
NameTypeDescriptionDefault
dataframeDataFrameInput DataFrame.required
value_columnstrName of the string column to analyze.required
Returns
TypeDescription
DataFrameOne-row DataFrame with the keys of summary_as_dict as columns.