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

Method decode

pandas/core/strings/accessor.py:2378–2434  ·  view source on GitHub ↗

Decode character string in the Series/Index using indicated encoding. Equivalent to :meth:`str.decode` in python2 and :meth:`bytes.decode` in python3. Parameters ---------- encoding : str Specifies the encoding to be used. errors

(
        self, encoding, errors: str = "strict", dtype: str | DtypeObj | None = None
    )

Source from the content-addressed store, hash-verified

2376 return self._wrap_result(result)
2377
2378 def decode(
2379 self, encoding, errors: str = "strict", dtype: str | DtypeObj | None = None
2380 ):
2381 """
2382 Decode character string in the Series/Index using indicated encoding.
2383
2384 Equivalent to :meth:`str.decode` in python2 and :meth:`bytes.decode` in
2385 python3.
2386
2387 Parameters
2388 ----------
2389 encoding : str
2390 Specifies the encoding to be used.
2391 errors : str, optional
2392 Specifies the error handling scheme.
2393 Possible values are those supported by :meth:`bytes.decode`.
2394 dtype : str or dtype, optional
2395 The dtype of the result. When not ``None``, must be either a string or
2396 object dtype. When ``None``, the dtype of the result is determined by
2397 ``pd.options.future.infer_string``.
2398
2399 .. versionadded:: 2.3.0
2400
2401 Returns
2402 -------
2403 Series or Index
2404 A Series or Index with decoded strings.
2405
2406 See Also
2407 --------
2408 Series.str.encode : Encodes strings into bytes in a Series/Index.
2409
2410 Examples
2411 --------
2412 For Series:
2413
2414 >>> ser = pd.Series([b"cow", b"123", b"()"])
2415 >>> ser.str.decode("ascii")
2416 0 cow
2417 1 123
2418 2 ()
2419 dtype: str
2420 """
2421 if dtype is not None and not is_string_dtype(dtype):
2422 raise ValueError(f"dtype must be string or object, got {dtype=}")
2423 if dtype is None and using_string_dtype():
2424 dtype = "str"
2425 # TODO: Add a similar _bytes interface.
2426 if encoding in _cpython_optimized_decoders:
2427 # CPython optimized implementation
2428 f = lambda x: x.decode(encoding, errors)
2429 else:
2430 decoder = codecs.getdecoder(encoding)
2431 f = lambda x: decoder(x, errors)[0]
2432 arr = self._data.array
2433 result = arr._str_map(f)
2434 return self._wrap_result(result, dtype=dtype)
2435

Callers 15

time_encode_decodeMethod · 0.80
get_authorsFunction · 0.80
run_commandFunction · 0.80
__call__Method · 0.80
ensure_decodedFunction · 0.80
string_column_to_ndarrayFunction · 0.80
ensure_strFunction · 0.80
_unconvert_string_arrayFunction · 0.80
_build_docMethod · 0.80
writeMethod · 0.80

Calls 4

_wrap_resultMethod · 0.95
is_string_dtypeFunction · 0.90
using_string_dtypeFunction · 0.90
_str_mapMethod · 0.45

Tested by 15

test_str_sizeFunction · 0.64
test_replace_unicodeFunction · 0.64
test_empty_str_methodsFunction · 0.64
test_encode_decodeFunction · 0.64
test_decode_errors_kwargFunction · 0.64
test_decode_string_dtypeFunction · 0.64
test_decode_object_dtypeFunction · 0.64
test_decode_bad_dtypeFunction · 0.64
test_series_str_decodeFunction · 0.64