pandas.Series.str¶
The attribute
pandas.Series.strcan only be used with Series ofstrvalues. You will either encounter anAttributionError(Can only use .str accessor with string values, which use np.object_ dtype in pandas) or find it to yield a Series ofNaN's if you invoke it on a Series of non-string values. If you have control of the DataFrame, the preferred way is to cast the type the column tostrin the DataFrame.df.status = df.status.astype(str)Generally speaking, it is a good idea to make sure that a column always have the same type in a pandas DataFrame. If you do not want to cast the column to
strin the DataFrame (for any reason), you can do this in computation without changing the type of the original column.df = df[df.status.astype(str).str.contains('Exit')]pandas.series.str.replacesupports regular expression.
import numpy as np
import pandas as pd
x = pd.Series([1, 2, 3])
x
Accessing .str with a Series of non-string values might throw AttributeError.
x.str
Try to invoke methods in pandas.Series.str on a Series of pathlib.Path yields a series of NaN's.
paths = pd.Series([Path("/root"), Path("abc.txt")])
paths
paths.str.upper()
A simple solution is to convert the type of the Series to str first and then call methods in pandas.Series.str.
paths.astype(str).str.upper()
s = pd.Series([np.nan, 1, 3, 10, 5])
s
s.sort_values()