Skip to content

Commit

Permalink
Backport PR #52032 on branch 2.0.x (CoW: Set copy=False explicitly in…
Browse files Browse the repository at this point in the history
…ternally 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 <61934744+phofl@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and phofl authored Mar 17, 2023
1 parent 065a320 commit 285a5aa
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -3125,7 +3125,7 @@ def read(
self.validate_read(columns, where)
index = self.read_index("index", start=start, stop=stop)
values = self.read_array("values", start=start, stop=stop)
return Series(values, index=index, name=self.name)
return Series(values, index=index, name=self.name, copy=False)

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

columns = items[items.get_indexer(blk_items)]
df = DataFrame(values.T, columns=columns, index=axes[1])
df = DataFrame(values.T, columns=columns, index=axes[1], copy=False)
dfs.append(df)

if len(dfs) > 0:
Expand Down Expand Up @@ -3460,7 +3460,7 @@ def write_metadata(self, key: str, values: np.ndarray) -> None:
"""
self.parent.put(
self._get_metadata_path(key),
Series(values),
Series(values, copy=False),
format="table",
encoding=self.encoding,
errors=self.errors,
Expand Down Expand Up @@ -4220,7 +4220,7 @@ def read_column(
encoding=self.encoding,
errors=self.errors,
)
return Series(_set_tz(col_values[1], a.tz), name=column)
return Series(_set_tz(col_values[1], a.tz), name=column, copy=False)

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

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

# delete the rows in reverse order
sorted_series = Series(values).sort_values()
sorted_series = Series(values, copy=False).sort_values()
ln = len(sorted_series)

if ln:
Expand Down Expand Up @@ -4560,7 +4560,7 @@ def read(
values = values.reshape((1, values.shape[0]))

if isinstance(values, np.ndarray):
df = DataFrame(values.T, columns=cols_, index=index_)
df = DataFrame(values.T, columns=cols_, index=index_, copy=False)
elif isinstance(values, Index):
df = DataFrame(values, columns=cols_, index=index_)
else:
Expand Down Expand Up @@ -5016,7 +5016,7 @@ def _convert_string_array(data: np.ndarray, encoding: str, errors: str) -> np.nd
# encode if needed
if len(data):
data = (
Series(data.ravel())
Series(data.ravel(), copy=False)
.str.encode(encoding, errors)
._values.reshape(data.shape)
)
Expand Down Expand Up @@ -5056,7 +5056,7 @@ def _unconvert_string_array(
dtype = f"U{itemsize}"

if isinstance(data[0], bytes):
data = Series(data).str.decode(encoding, errors=errors)._values
data = Series(data, copy=False).str.decode(encoding, errors=errors)._values
else:
data = data.astype(dtype, copy=False).astype(object, copy=False)

Expand Down

0 comments on commit 285a5aa

Please sign in to comment.