|
1 | 1 | from collections import OrderedDict
|
| 2 | +import functools |
2 | 3 | from io import StringIO
|
3 | 4 | from itertools import islice
|
4 | 5 | import os
|
@@ -1005,43 +1006,51 @@ class SeriesParser(Parser):
|
1005 | 1006 |
|
1006 | 1007 | def _parse_no_numpy(self):
|
1007 | 1008 |
|
1008 |
| - json = self.json |
1009 |
| - orient = self.orient |
1010 |
| - if orient == "split": |
1011 |
| - decoded = { |
1012 |
| - str(k): v |
1013 |
| - for k, v in loads(json, precise_float=self.precise_float).items() |
1014 |
| - } |
| 1009 | + data = loads(self.json, precise_float=self.precise_float) |
| 1010 | + |
| 1011 | + if self.orient == "split": |
| 1012 | + is_empty = self._is_empty(data["data"]) |
| 1013 | + else: |
| 1014 | + is_empty = self._is_empty(data) |
| 1015 | + dtype = object if is_empty else None |
| 1016 | + |
| 1017 | + if self.orient == "split": |
| 1018 | + decoded = {str(k): v for k, v in data.items()} |
1015 | 1019 | self.check_keys_split(decoded)
|
1016 |
| - self.obj = Series(dtype=None, **decoded) |
| 1020 | + self.obj = Series(**decoded, dtype=dtype) |
1017 | 1021 | else:
|
1018 |
| - self.obj = Series(loads(json, precise_float=self.precise_float), dtype=None) |
| 1022 | + self.obj = Series(data, dtype=dtype) |
1019 | 1023 |
|
1020 | 1024 | def _parse_numpy(self):
|
1021 | 1025 |
|
1022 |
| - json = self.json |
1023 |
| - orient = self.orient |
1024 |
| - if orient == "split": |
1025 |
| - decoded = loads( |
1026 |
| - json, dtype=None, numpy=True, precise_float=self.precise_float |
1027 |
| - ) |
1028 |
| - decoded = {str(k): v for k, v in decoded.items()} |
| 1026 | + kwargs = {"dtype": None, "numpy": True, "precise_float": self.precise_float} |
| 1027 | + if self.orient in ["columns", "index"]: |
| 1028 | + kwargs["labelled"] = True |
| 1029 | + loads_ = functools.partial(loads, **kwargs) |
| 1030 | + data = loads_(self.json) |
| 1031 | + |
| 1032 | + # this is needed to silence a FutureWarning |
| 1033 | + # TODO: Remove this when the default dtype of empty Series is changed to object |
| 1034 | + if self.orient == "split": |
| 1035 | + is_empty = self._is_empty(data["data"]) |
| 1036 | + else: |
| 1037 | + is_empty = self._is_empty(data) |
| 1038 | + dtype = object if is_empty else None |
| 1039 | + |
| 1040 | + if self.orient == "split": |
| 1041 | + decoded = {str(k): v for k, v in data.items()} |
1029 | 1042 | self.check_keys_split(decoded)
|
1030 |
| - self.obj = Series(**decoded) |
1031 |
| - elif orient == "columns" or orient == "index": |
1032 |
| - self.obj = Series( |
1033 |
| - *loads( |
1034 |
| - json, |
1035 |
| - dtype=None, |
1036 |
| - numpy=True, |
1037 |
| - labelled=True, |
1038 |
| - precise_float=self.precise_float, |
1039 |
| - ) |
1040 |
| - ) |
| 1043 | + self.obj = Series(**decoded, dtype=dtype) |
| 1044 | + elif self.orient in ["columns", "index"]: |
| 1045 | + self.obj = Series(*data, dtype=dtype) |
1041 | 1046 | else:
|
1042 |
| - self.obj = Series( |
1043 |
| - loads(json, dtype=None, numpy=True, precise_float=self.precise_float) |
1044 |
| - ) |
| 1047 | + self.obj = Series(data, dtype=dtype) |
| 1048 | + |
| 1049 | + @staticmethod |
| 1050 | + def _is_empty(data): |
| 1051 | + is_empty_np = isinstance(data, np.ndarray) and (data.size == 0) |
| 1052 | + is_empty_reg = isinstance(data, (list, tuple, dict)) and not data |
| 1053 | + return is_empty_np or is_empty_reg |
1045 | 1054 |
|
1046 | 1055 | def _try_convert_types(self):
|
1047 | 1056 | if self.obj is None:
|
|
0 commit comments