Skip to content

REF: eliminate method _write() in json writers #36218

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 11 commits into from
Oct 22, 2020
130 changes: 45 additions & 85 deletions pandas/io/json/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,74 +145,82 @@ def _format_axes(self):
raise AbstractMethodError(self)

def write(self):
return self._write(
iso_dates = self.date_format == "iso"
return dumps(
self.obj,
self.orient,
self.double_precision,
self.ensure_ascii,
self.date_unit,
self.date_format == "iso",
self.default_handler,
self.indent,
orient=self.orient,
double_precision=self.double_precision,
ensure_ascii=self.ensure_ascii,
date_unit=self.date_unit,
iso_dates=iso_dates,
default_handler=self.default_handler,
indent=self.indent,
)

def _write(

class SeriesWriter(Writer):
_default_orient = "index"

def __init__(
self,
obj,
orient: Optional[str],
date_format: str,
double_precision: int,
ensure_ascii: bool,
date_unit: str,
iso_dates: bool,
default_handler: Optional[Callable[[Any], JSONSerializable]],
indent: int,
index: bool,
default_handler: Optional[Callable[[Any], JSONSerializable]] = None,
indent: int = 0,
):
return dumps(
super().__init__(
obj,
orient=orient,
double_precision=double_precision,
ensure_ascii=ensure_ascii,
date_unit=date_unit,
iso_dates=iso_dates,
orient,
date_format,
double_precision,
ensure_ascii,
date_unit,
index,
default_handler=default_handler,
indent=indent,
)


class SeriesWriter(Writer):
_default_orient = "index"
if not index and orient == "split":
self.obj = {"name": obj.name, "data": obj.values}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm why do we need to do this now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not know. I simply used the original implementation and moved whatever assignments were in _write into __init__.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh OK - I see what was done here. Let's not assign to self.obj as that will mess with the idempotency

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We assign to self.obj at the moment of the writer initialization, not when calling a method. So, I would argue that it is not harmful.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think it is confusing to change what self.obj is for certain orients - would prefer not to do this


def _format_axes(self):
if not self.obj.index.is_unique and self.orient == "index":
raise ValueError(f"Series index must be unique for orient='{self.orient}'")

def _write(

class FrameWriter(Writer):
_default_orient = "columns"

def __init__(
self,
obj,
orient: Optional[str],
date_format: str,
double_precision: int,
ensure_ascii: bool,
date_unit: str,
iso_dates: bool,
default_handler: Optional[Callable[[Any], JSONSerializable]],
indent: int,
index: bool,
default_handler: Optional[Callable[[Any], JSONSerializable]] = None,
indent: int = 0,
):
if not self.index and orient == "split":
obj = {"name": obj.name, "data": obj.values}
return super()._write(
super().__init__(
obj,
orient,
date_format,
double_precision,
ensure_ascii,
date_unit,
iso_dates,
default_handler,
indent,
index,
default_handler=default_handler,
indent=indent,
)


class FrameWriter(Writer):
_default_orient = "columns"
if not index and orient == "split":
self.obj = obj.to_dict(orient="split")
del self.obj["index"]

def _format_axes(self):
"""
Expand All @@ -231,31 +239,6 @@ def _format_axes(self):
f"DataFrame columns must be unique for orient='{self.orient}'."
)

def _write(
self,
obj,
orient: Optional[str],
double_precision: int,
ensure_ascii: bool,
date_unit: str,
iso_dates: bool,
default_handler: Optional[Callable[[Any], JSONSerializable]],
indent: int,
):
if not self.index and orient == "split":
obj = obj.to_dict(orient="split")
del obj["index"]
return super()._write(
obj,
orient,
double_precision,
ensure_ascii,
date_unit,
iso_dates,
default_handler,
indent,
)


class JSONTableWriter(FrameWriter):
_default_orient = "records"
Expand Down Expand Up @@ -330,30 +313,7 @@ def __init__(
self.orient = "records"
self.index = index

def _write(
self,
obj,
orient,
double_precision,
ensure_ascii,
date_unit,
iso_dates,
default_handler,
indent,
):
table_obj = {"schema": self.schema, "data": obj}
serialized = super()._write(
table_obj,
orient,
double_precision,
ensure_ascii,
date_unit,
iso_dates,
default_handler,
indent,
)

return serialized
self.obj = {"schema": self.schema, "data": self.obj}


@deprecate_kwarg(old_arg_name="numpy", new_arg_name=None)
Expand Down