MCPcopy
hub / github.com/pandas-dev/pandas / partition

Method partition

pandas/core/strings/accessor.py:1031–1120  ·  view source on GitHub ↗

Split the string at the first occurrence of `sep`. This method splits the string at the first occurrence of `sep`, and returns 3 elements containing the part before the separator, the separator itself, and the part after the separator. If the separator is no

(self, sep: str = " ", expand: bool = True)

Source from the content-addressed store, hash-verified

1029
1030 @forbid_nonstring_types(["bytes"])
1031 def partition(self, sep: str = " ", expand: bool = True):
1032 """
1033 Split the string at the first occurrence of `sep`.
1034
1035 This method splits the string at the first occurrence of `sep`,
1036 and returns 3 elements containing the part before the separator,
1037 the separator itself, and the part after the separator.
1038 If the separator is not found, return 3 elements containing the string itself,
1039 followed by two empty strings.
1040
1041 Parameters
1042 ----------
1043 sep : str, default whitespace
1044 String to split on.
1045 expand : bool, default True
1046 If True, return DataFrame/MultiIndex expanding dimensionality.
1047 If False, return Series/Index.
1048
1049 Returns
1050 -------
1051 DataFrame/MultiIndex or Series/Index of objects
1052 Returns appropriate type based on `expand` parameter with strings
1053 split based on the `sep` parameter.
1054
1055 See Also
1056 --------
1057 rpartition : Split the string at the last occurrence of `sep`.
1058 Series.str.split : Split strings around given separators.
1059 str.partition : Standard library version.
1060
1061 Examples
1062 --------
1063 >>> s = pd.Series(["Linda van der Berg", "George Pitt-Rivers"])
1064 >>> s
1065 0 Linda van der Berg
1066 1 George Pitt-Rivers
1067 dtype: str
1068
1069 >>> s.str.partition()
1070 0 1 2
1071 0 Linda van der Berg
1072 1 George Pitt-Rivers
1073
1074 To partition by the last space instead of the first one:
1075
1076 >>> s.str.rpartition()
1077 0 1 2
1078 0 Linda van der Berg
1079 1 George Pitt-Rivers
1080
1081 To partition by something different than a space:
1082
1083 >>> s.str.partition("-")
1084 0 1 2
1085 0 Linda van der Berg
1086 1 George Pitt - Rivers
1087
1088 To return a Series containing tuples instead of a DataFrame:

Callers 13

time_partitionMethod · 0.80
_str_partitionMethod · 0.80
_str_partitionMethod · 0.80
_decodeMethod · 0.80
detect_colspecsMethod · 0.80
parseMethod · 0.80
test_empty_str_methodsFunction · 0.80
test_partition_with_nameFunction · 0.80
create_mgrFunction · 0.80

Calls 2

_wrap_resultMethod · 0.95
_str_partitionMethod · 0.45

Tested by 7

test_empty_str_methodsFunction · 0.64
test_partition_with_nameFunction · 0.64
create_mgrFunction · 0.64
test_str_partitionFunction · 0.64