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
)
| 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 |