Skip to content

Commit 3bb9e62

Browse files
authored
REF: dont pass downcast=False to fillna from pytables (#45010)
1 parent b1a2f48 commit 3bb9e62

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

pandas/io/pytables.py

+9-16
Original file line numberDiff line numberDiff line change
@@ -3950,7 +3950,7 @@ def _create_axes(
39503950
new_name = name or f"values_block_{i}"
39513951
data_converted = _maybe_convert_for_string_atom(
39523952
new_name,
3953-
blk,
3953+
blk.values,
39543954
existing_col=existing_col,
39553955
min_itemsize=min_itemsize,
39563956
nan_rep=nan_rep,
@@ -4933,19 +4933,20 @@ def _unconvert_index(data, kind: str, encoding: str, errors: str) -> np.ndarray
49334933

49344934
def _maybe_convert_for_string_atom(
49354935
name: str,
4936-
block: Block,
4936+
bvalues: ArrayLike,
49374937
existing_col,
49384938
min_itemsize,
49394939
nan_rep,
49404940
encoding,
49414941
errors,
49424942
columns: list[str],
49434943
):
4944-
bvalues = block.values
49454944

49464945
if bvalues.dtype != object:
49474946
return bvalues
49484947

4948+
bvalues = cast(np.ndarray, bvalues)
4949+
49494950
dtype_name = bvalues.dtype.name
49504951
inferred_type = lib.infer_dtype(bvalues, skipna=False)
49514952

@@ -4961,13 +4962,9 @@ def _maybe_convert_for_string_atom(
49614962
elif not (inferred_type == "string" or dtype_name == "object"):
49624963
return bvalues
49634964

4964-
blocks: list[Block] = block.fillna(nan_rep, downcast=False)
4965-
# Note: because block is always object dtype, fillna goes
4966-
# through a path such that the result is always a 1-element list
4967-
assert len(blocks) == 1
4968-
block = blocks[0]
4969-
4970-
data = block.values
4965+
mask = isna(bvalues)
4966+
data = bvalues.copy()
4967+
data[mask] = nan_rep
49714968

49724969
# see if we have a valid string type
49734970
inferred_type = lib.infer_dtype(data, skipna=False)
@@ -4979,7 +4976,7 @@ def _maybe_convert_for_string_atom(
49794976
# expected behaviour:
49804977
# search block for a non-string object column by column
49814978
for i in range(data.shape[0]):
4982-
col = block.iget(i)
4979+
col = data[i]
49834980
inferred_type = lib.infer_dtype(col, skipna=False)
49844981
if inferred_type != "string":
49854982
error_column_label = columns[i] if len(columns) > i else f"No.{i}"
@@ -4991,11 +4988,7 @@ def _maybe_convert_for_string_atom(
49914988

49924989
# itemsize is the maximum length of a string (along any dimension)
49934990

4994-
# error: Argument 1 to "_convert_string_array" has incompatible type "Union[ndarray,
4995-
# ExtensionArray]"; expected "ndarray"
4996-
data_converted = _convert_string_array(
4997-
data, encoding, errors # type: ignore[arg-type]
4998-
).reshape(data.shape)
4991+
data_converted = _convert_string_array(data, encoding, errors).reshape(data.shape)
49994992
itemsize = data_converted.itemsize
50004993

50014994
# specified min_itemsize?

0 commit comments

Comments
 (0)