Pandas Series.str.fullmatch() Method



The Series.str.fullmatch() method in Pandas is used to determine if each string in the Series entirely matches a specified regular expression pattern or not.

This method is useful when you want to verify if entire strings conform to a given format or pattern. And equivalent to applying re.fullmatch() to each string in the Series.

Syntax

Following is the syntax of the Pandas Series.str.fullmatch() method −

Series.str.fullmatch(pat, case=True, flags=0, na=None)

Parameters

The Series.str.fullmatch() method accepts the following parameters −

  • pat − A string representing the character sequence or regular expression pattern to match against.

  • case − A boolean value, default is True. If True, the match is case sensitive.

  • flags − An optional integer, default is 0. Flags from the re module, such as re.IGNORECASE, to modify the pattern matching behavior.

  • na − An optional scalar value used for missing values. If not specified, the default depends on the dtype of the Series. For object-dtype, numpy.nan is used. For StringDtype, pandas.NA is used.

Return Value

The Series.str.fullmatch() method returns a Series or Index of boolean values. Each boolean value indicates whether the corresponding string in the Series entirely matches the given regular expression pattern.

Example 1

This example demonstrates checking if each string in a Series fully matches the regular expression pattern for a valid email address.

import pandas as pd

# Create a Series of strings
s = pd.Series(['user@example.com', 'user@domain', 'example.com', 'test@tutorialspoint.com'])

# Check if each string fully matches the pattern for an email address
result = s.str.fullmatch(r'\w+@\w+\.\w+')

print("Input Series:")
print(s)
print("\nFull Match Results:")
print(result)

When we run the above code, it produces the following output −

Input Series:
0           user@example.com
1                user@domain
2                example.com
3    test@tutorialspoint.com
dtype: object

Full Match Results:
0     True
1    False
2    False
3     True
dtype: bool

The output shows that only the strings that fully match the email pattern are marked as True.

Example 2

This example demonstrates checking if each string fully matches the pattern for a date in the format 'YYYY-MM-DD' using the Series.str.fullmatch() method.

import pandas as pd

# Create a Series of strings
s = pd.Series(['2024-07-29', '2024-07-29 00:00:00', '2024-07-29T00:00:00', '07-29-2024'])

# Check if each string fully matches the date pattern
result = s.str.fullmatch(r'\d{4}-\d{2}-\d{2}')

print("Input Series:")
print(s)
print("\nFull Match Results:")
print(result)

When we run the above code, it produces the following output −

Input Series:
0             2024-07-29
1    2024-07-29 00:00:00
2    2024-07-29T00:00:00
3             07-29-2024
dtype: object

Full Match Results:
0     True
1    False
2    False
3    False
dtype: bool

Example 3

This example demonstrates checking if each string in a DataFrame column fully matches a date pattern, while handling missing values.

import pandas as pd

# Create a DataFrame with date strings
df = pd.DataFrame({
    'date': ['2024-07-29', '2024-07-29 00:00:00', '2024-07-29', None]
})

# Check if each string fully matches the date pattern, treating NaNs as True
result = df['date'].str.fullmatch(r'\d{4}-\d{2}-\d{2}', na=True)

print("Input DataFrame:")
print(df)
print("\nFull Match Results:")
print(result)

When we run the above code, it produces the following output −

Input DataFrame:
                  date
0           2024-07-29
1  2024-07-29 00:00:00
2           2024-07-29
3                 None

Full Match Results:
0     True
1    False
2     True
3     True
Name: date, dtype: bool

In this case, the NaN value is treated as True due to the na=True parameter, while other strings are matched according to the pattern.

python_pandas_working_with_text_data.htm
Advertisements
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy