Skip to content

Commit

Permalink
Fixup JSON take
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAugspurger committed Apr 25, 2018
1 parent 0be9ec6 commit 08f2479
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions pandas/tests/extension/json/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
class JSONDtype(ExtensionDtype):
type = collections.Mapping
name = 'json'
na_value = {}

@classmethod
def construct_from_string(cls, string):
Expand Down Expand Up @@ -91,15 +92,30 @@ def nbytes(self):
return sys.getsizeof(self.data)

def isna(self):
return np.array([x == self._na_value for x in self.data])

def take(self, indexer, allow_fill=True, fill_value=None):
try:
output = [self.data[loc] if loc != -1 else self._na_value
for loc in indexer]
except IndexError:
raise IndexError("Index is out of bounds or cannot do a "
"non-empty take from an empty array.")
return np.array([x == self.dtype.na_value for x in self.data])

def take(self, indexer, fill_value=None):
# re-implement here, since NumPy has trouble setting
# sized objects like UserDicts into scalar slots of
# an ndarary.
indexer = np.asarray(indexer)
msg = ("Index is out of bounds or cannot do a "
"non-empty take from an empty array.")

if fill_value is None:
try:
output = [self.data[loc] for loc in indexer]
except IndexError:
raise IndexError(msg)
else:
# bounds check
if (indexer < -1).any():
raise ValueError
try:
output = [self.data[loc] if loc != -1 else fill_value
for loc in indexer]
except IndexError:
raise msg
return self._from_sequence(output)

def copy(self, deep=False):
Expand All @@ -112,10 +128,6 @@ def unique(self):
dict(x) for x in list(set(tuple(d.items()) for d in self.data))
])

@property
def _na_value(self):
return {}

@classmethod
def _concat_same_type(cls, to_concat):
data = list(itertools.chain.from_iterable([x.data for x in to_concat]))
Expand Down

0 comments on commit 08f2479

Please sign in to comment.