Skip to content

PERF: Use shallow copies/defer copies in io #58960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions pandas/io/json/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,18 +369,22 @@ def __init__(
msg = "Overlapping names between the index and columns"
raise ValueError(msg)

obj = obj.copy()
timedeltas = obj.select_dtypes(include=["timedelta"]).columns
copied = False
if len(timedeltas):
obj = obj.copy()
copied = True
obj[timedeltas] = obj[timedeltas].map(lambda x: x.isoformat())
# Convert PeriodIndex to datetimes before serializing
if isinstance(obj.index.dtype, PeriodDtype):
obj.index = obj.index.to_timestamp()

# exclude index from obj if index=False
if not self.index:
self.obj = obj.reset_index(drop=True)
else:
# Convert PeriodIndex to datetimes before serializing
if isinstance(obj.index.dtype, PeriodDtype):
if not copied:
obj = obj.copy(deep=False)
obj.index = obj.index.to_timestamp()
self.obj = obj.reset_index(drop=False)
self.date_format = "iso"
self.orient = "records"
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/json/_table_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def set_default_names(data):
)
return data

data = data.copy()
data = data.copy(deep=False)
if data.index.nlevels > 1:
data.index.names = com.fill_missing_names(data.index.names)
else:
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ def _execute_insert_multi(self, conn, keys: list[str], data_iter) -> int:

def insert_data(self) -> tuple[list[str], list[np.ndarray]]:
if self.index is not None:
temp = self.frame.copy()
temp = self.frame.copy(deep=False)
temp.index.names = self.index
try:
temp.reset_index(inplace=True)
Expand Down