Merged
Show file tree
Hide file tree
Changes from all commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Failed to load files.
Original file line numberDiff line numberDiff line change
Expand Up@@ -259,6 +259,7 @@ def _wrap_result(
fill_value=np.nan,
returns_string: bool = True,
returns_bool: bool = False,
dtype=None,
):
from pandas import (
Index,
Expand DownExpand Up@@ -379,29 +380,29 @@ def cons_row(x):
out = out.get_level_values(0)
return out
else:
return Index(result, name=name)
return Index(result, name=name, dtype=dtype)
else:
index = self._orig.index
# This is a mess.
dtype: DtypeObj | str | None
_dtype: DtypeObj | str | None = dtype
vdtype = getattr(result, "dtype", None)
if self._is_string:
if is_bool_dtype(vdtype):
dtype = result.dtype
_dtype = result.dtype
elif returns_string:
dtype = self._orig.dtype
_dtype = self._orig.dtype
else:
dtype = vdtype
else:
dtype = vdtype
_dtype = vdtype
elif vdtype is not None:
_dtype = vdtype

if expand:
cons = self._orig._constructor_expanddim
result = cons(result, columns=name, index=index, dtype=dtype)
result = cons(result, columns=name, index=index, dtype=_dtype)
else:
# Must be a Series
cons = self._orig._constructor
result = cons(result, name=name, index=index, dtype=dtype)
result = cons(result, name=name, index=index, dtype=_dtype)
result = result.__finalize__(self._orig, method="str")
if name is not None and result.ndim == 1:
# __finalize__ might copy over the original name, but we may
Expand DownExpand Up@@ -2317,7 +2318,8 @@ def translate(self, table):
dtype: object
"""
result = self._data.array._str_translate(table)
return self._wrap_result(result)
dtype = object if self._data.dtype == "object" else None
return self._wrap_result(result, dtype=dtype)
Copy link
Member

@mroeschke mroeschke Nov 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe dumb question, but we can't just pass dtype=self._data.dtype and have the result dtype be correct in _wrap_result (with maybe additional logic)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Categoricals act out then, that's a bit confusing to me as well, but I didn't want to focus on this now.


@forbid_nonstring_types(["bytes"])
def count(self, pat, flags: int = 0):
Expand Down