Skip to content

Commit

Permalink
PERF: Use shallow copies/defer copies in io (#58960)
Browse files Browse the repository at this point in the history
  • Loading branch information
mroeschke committed Jun 14, 2024
1 parent 0fb5cfe commit 3bcc95f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
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

0 comments on commit 3bcc95f

Please sign in to comment.