-
-
Notifications
You must be signed in to change notification settings - Fork 18k
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
TYP: io.json._json, util._decorators #36903
Changes from all commits
5c9340b
4017681
3b1fba6
5347e16
44ebc7e
c5bbdfd
f152ca8
22e8da9
a1fb8b6
88aa019
1ec0fc2
ea54168
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
from io import BytesIO, StringIO | ||
from itertools import islice | ||
import os | ||
from typing import IO, Any, Callable, List, Optional, Type | ||
from typing import IO, Any, Callable, List, Optional, Tuple, Type | ||
|
||
import numpy as np | ||
|
||
|
@@ -111,6 +111,8 @@ def to_json( | |
|
||
|
||
class Writer: | ||
_default_orient: str | ||
|
||
def __init__( | ||
self, | ||
obj, | ||
|
@@ -126,8 +128,7 @@ def __init__( | |
self.obj = obj | ||
|
||
if orient is None: | ||
# error: "Writer" has no attribute "_default_orient" | ||
orient = self._default_orient # type: ignore[attr-defined] | ||
orient = self._default_orient | ||
|
||
self.orient = orient | ||
self.date_format = date_format | ||
|
@@ -777,8 +778,8 @@ def read(self): | |
obj = self._get_object_parser(lines_json) | ||
else: | ||
data = ensure_str(self.data) | ||
data = data.split("\n") | ||
obj = self._get_object_parser(self._combine_lines(data)) | ||
data_lines = data.split("\n") | ||
obj = self._get_object_parser(self._combine_lines(data_lines)) | ||
else: | ||
obj = self._get_object_parser(self.data) | ||
self.close() | ||
|
@@ -848,6 +849,8 @@ def __next__(self): | |
|
||
|
||
class Parser: | ||
_split_keys: Tuple[str, ...] | ||
_default_orient: str | ||
|
||
_STAMP_UNITS = ("s", "ms", "us", "ns") | ||
_MIN_STAMPS = { | ||
|
@@ -873,6 +876,7 @@ def __init__( | |
|
||
if orient is None: | ||
orient = self._default_orient | ||
|
||
self.orient = orient | ||
|
||
self.dtype = dtype | ||
|
@@ -902,8 +906,8 @@ def check_keys_split(self, decoded): | |
""" | ||
bad_keys = set(decoded.keys()).difference(set(self._split_keys)) | ||
if bad_keys: | ||
bad_keys = ", ".join(bad_keys) | ||
raise ValueError(f"JSON data had unexpected key(s): {bad_keys}") | ||
bad_keys_joined = ", ".join(bad_keys) | ||
raise ValueError(f"JSON data had unexpected key(s): {bad_keys_joined}") | ||
|
||
def parse(self): | ||
|
||
|
@@ -922,14 +926,22 @@ def parse(self): | |
self._try_convert_types() | ||
return self.obj | ||
|
||
def _parse_numpy(self): | ||
raise AbstractMethodError(self) | ||
|
||
def _parse_no_numpy(self): | ||
raise AbstractMethodError(self) | ||
|
||
def _convert_axes(self): | ||
""" | ||
Try to convert axes. | ||
""" | ||
for axis_name in self.obj._AXIS_ORDERS: | ||
obj = self.obj | ||
assert obj is not None # for mypy | ||
for axis_name in obj._AXIS_ORDERS: | ||
new_axis, result = self._try_convert_data( | ||
name=axis_name, | ||
data=self.obj._get_axis(axis_name), | ||
data=obj._get_axis(axis_name), | ||
use_dtypes=False, | ||
convert_dates=True, | ||
) | ||
|
@@ -1083,7 +1095,11 @@ def _parse_numpy(self): | |
self.check_keys_split(decoded) | ||
self.obj = create_series_with_explicit_dtype(**decoded) | ||
elif self.orient in ["columns", "index"]: | ||
self.obj = create_series_with_explicit_dtype(*data, dtype_if_empty=object) | ||
# error: "create_series_with_explicit_dtype" | ||
# gets multiple values for keyword argument "dtype_if_empty | ||
self.obj = create_series_with_explicit_dtype( | ||
*data, dtype_if_empty=object | ||
) # type:ignore[misc] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the default for dtype_if_empty is object, so do we need to pass dtype_if_empty. if data has more than six items, this would indeed raise.
do we know what data contains? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not off the top of my head. @WillAyd any idea? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure. Assuming this path comes from the |
||
else: | ||
self.obj = create_series_with_explicit_dtype(data, dtype_if_empty=object) | ||
|
||
|
@@ -1175,9 +1191,12 @@ def _process_converter(self, f, filt=None): | |
if filt is None: | ||
filt = lambda col, c: True | ||
|
||
obj = self.obj | ||
assert obj is not None # for mypy | ||
|
||
needs_new_obj = False | ||
new_obj = dict() | ||
for i, (col, c) in enumerate(self.obj.items()): | ||
for i, (col, c) in enumerate(obj.items()): | ||
if filt(col, c): | ||
new_data, result = f(col, c) | ||
if result: | ||
|
@@ -1188,9 +1207,9 @@ def _process_converter(self, f, filt=None): | |
if needs_new_obj: | ||
|
||
# possibly handle dup columns | ||
new_obj = DataFrame(new_obj, index=self.obj.index) | ||
new_obj.columns = self.obj.columns | ||
self.obj = new_obj | ||
new_frame = DataFrame(new_obj, index=obj.index) | ||
new_frame.columns = obj.columns | ||
self.obj = new_frame | ||
|
||
def _try_convert_types(self): | ||
if self.obj is None: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can do it here or as follow-on, but we should do the same for Writer and remove the ignore there.