Skip to content

Commit 285a5aa

Browse files
Backport PR #52032 on branch 2.0.x (CoW: Set copy=False explicitly internally for Series and DataFrame in io/pytables) (#52049)
Backport PR #52032: CoW: Set copy=False explicitly internally for Series and DataFrame in io/pytables Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 065a320 commit 285a5aa

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

pandas/io/pytables.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -3125,7 +3125,7 @@ def read(
31253125
self.validate_read(columns, where)
31263126
index = self.read_index("index", start=start, stop=stop)
31273127
values = self.read_array("values", start=start, stop=stop)
3128-
return Series(values, index=index, name=self.name)
3128+
return Series(values, index=index, name=self.name, copy=False)
31293129

31303130
# error: Signature of "write" incompatible with supertype "Fixed"
31313131
def write(self, obj, **kwargs) -> None: # type: ignore[override]
@@ -3192,7 +3192,7 @@ def read(
31923192
values = self.read_array(f"block{i}_values", start=_start, stop=_stop)
31933193

31943194
columns = items[items.get_indexer(blk_items)]
3195-
df = DataFrame(values.T, columns=columns, index=axes[1])
3195+
df = DataFrame(values.T, columns=columns, index=axes[1], copy=False)
31963196
dfs.append(df)
31973197

31983198
if len(dfs) > 0:
@@ -3460,7 +3460,7 @@ def write_metadata(self, key: str, values: np.ndarray) -> None:
34603460
"""
34613461
self.parent.put(
34623462
self._get_metadata_path(key),
3463-
Series(values),
3463+
Series(values, copy=False),
34643464
format="table",
34653465
encoding=self.encoding,
34663466
errors=self.errors,
@@ -4220,7 +4220,7 @@ def read_column(
42204220
encoding=self.encoding,
42214221
errors=self.errors,
42224222
)
4223-
return Series(_set_tz(col_values[1], a.tz), name=column)
4223+
return Series(_set_tz(col_values[1], a.tz), name=column, copy=False)
42244224

42254225
raise KeyError(f"column [{column}] not found in the table")
42264226

@@ -4447,7 +4447,7 @@ def delete(self, where=None, start: int | None = None, stop: int | None = None):
44474447
values = selection.select_coords()
44484448

44494449
# delete the rows in reverse order
4450-
sorted_series = Series(values).sort_values()
4450+
sorted_series = Series(values, copy=False).sort_values()
44514451
ln = len(sorted_series)
44524452

44534453
if ln:
@@ -4560,7 +4560,7 @@ def read(
45604560
values = values.reshape((1, values.shape[0]))
45614561

45624562
if isinstance(values, np.ndarray):
4563-
df = DataFrame(values.T, columns=cols_, index=index_)
4563+
df = DataFrame(values.T, columns=cols_, index=index_, copy=False)
45644564
elif isinstance(values, Index):
45654565
df = DataFrame(values, columns=cols_, index=index_)
45664566
else:
@@ -5016,7 +5016,7 @@ def _convert_string_array(data: np.ndarray, encoding: str, errors: str) -> np.nd
50165016
# encode if needed
50175017
if len(data):
50185018
data = (
5019-
Series(data.ravel())
5019+
Series(data.ravel(), copy=False)
50205020
.str.encode(encoding, errors)
50215021
._values.reshape(data.shape)
50225022
)
@@ -5056,7 +5056,7 @@ def _unconvert_string_array(
50565056
dtype = f"U{itemsize}"
50575057

50585058
if isinstance(data[0], bytes):
5059-
data = Series(data).str.decode(encoding, errors=errors)._values
5059+
data = Series(data, copy=False).str.decode(encoding, errors=errors)._values
50605060
else:
50615061
data = data.astype(dtype, copy=False).astype(object, copy=False)
50625062

0 commit comments

Comments
 (0)