-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Error Indexing DafaFrame with a 0-d array #21946
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
Comments
cc @jreback |
FYI #21861 brought this closer - In [3]: >>> df.iloc[ar]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-9eb33c655c7c> in <module>()
----> 1 df.iloc[ar]
~\Documents\python-dev\pandas\pandas\core\indexing.py in __getitem__(self, key)
1508
1509 maybe_callable = com._apply_if_callable(key, self.obj)
-> 1510 return self._getitem_axis(maybe_callable, axis=axis)
1511
1512 def _is_scalar_access(self, key):
~\Documents\python-dev\pandas\pandas\core\indexing.py in _getitem_axis(self, key, axis)
2216 # a single integer
2217 else:
-> 2218 key = self._convert_scalar_indexer(key, axis)
2219
2220 if not is_integer(key):
~\Documents\python-dev\pandas\pandas\core\indexing.py in _convert_scalar_indexer(self, key, axis)
264 ax = self.obj._get_axis(min(axis, self.ndim - 1))
265 # a scalar
--> 266 return ax._convert_scalar_indexer(key, kind=self.name)
267
268 def _convert_slice_indexer(self, key, axis):
~\Documents\python-dev\pandas\pandas\core\indexes\numeric.py in _convert_scalar_indexer(self, key, kind)
185 key = self._maybe_cast_indexer(key)
186 return (super(Int64Index, self)
--> 187 ._convert_scalar_indexer(key, kind=kind))
188
189 def _wrap_joined_index(self, joined, other):
~\Documents\python-dev\pandas\pandas\core\indexes\base.py in _convert_scalar_indexer(self, key, kind)
1540
1541 if kind == 'iloc':
-> 1542 return self._validate_indexer('positional', key, kind)
1543
1544 if len(self) and not isinstance(self, ABCMultiIndex,):
~\Documents\python-dev\pandas\pandas\core\indexes\base.py in _validate_indexer(self, form, key, kind)
4158 pass
4159 elif kind in ['iloc', 'getitem']:
-> 4160 self._invalid_indexer(form, key)
4161 return key
4162
~\Documents\python-dev\pandas\pandas\core\indexes\base.py in _invalid_indexer(self, form, key)
1751 "indexers [{key}] of {kind}".format(
1752 form=form, klass=type(self), key=key,
-> 1753 kind=type(key)))
1754
1755 def get_duplicates(self):
TypeError: cannot do positional indexing on <class 'pandas.core.indexes.range.RangeIndex'> with these indexers [0] of <class 'numpy.ndarray'> |
I don't think #21861 is a good fix here. return (isinstance(obj, Iterable) and
# we do not count strings/unicode/bytes as list-like
not isinstance(obj, string_and_binary_types) and
# exclude zero-dimensional numpy arrays, effectively scalars
not (isinstance(obj, np.ndarray) and obj.ndim == 0)) The last line looks like a bad decision. >>> import torch
>>> tn = torch.tensor(0)
>>> df.iloc[tn]
Traceback (most recent call last):
...
...
File "/home/float2/Documents/Python/Pandas/pandas/pandas/core/indexes/base.py", line 528, in _shallow_copy_with_infer
if not len(values) and 'dtype' not in kwargs:
TypeError: object of type 'numpy.int64' has no len() The Error hasn't changed and now, after #21861, two issues of the same nature has different output errors. |
@floatn, welcome a PR if you have a suggestion on how we should handle this, 0d arrays are an odd case by themselves, along with the complication of duck array types. |
@floatn what behavior do you think makes sense with a zero-dim array? Does torch.tensor subclass ndarray or otherwise present "standard" attributes that we can use to tell we are looking at a pseudo-scalar? |
@jbrockmendel |
Is anyone working on this? I'd love to make a contribution if it's as simple as what you discussed. |
@illegalnumbers : I don't think so. Go for it! |
Cool I'm gonna start working on this now and hopefully have a PR today. |
It seems that |
There are two ways to look at this:
Your call! |
Calling |
Well in that case calling |
Hmm |
closes pandas-dev#21946 Change-Id: I79cb68527820adddd3a8e3f4a71c12ee677eec60
I think #22032 should do it :) (pending full specs pass) |
So referenced in my pull request I mention that it appears this functionality is fixed as appropriate on master. |
Is there any reason this issue should not be closed? |
@jreback @jorisvandenbossche I have no permission to close this issue, so please close. |
@tamuhey issues are closed when the corresponding PR is actually merged |
When I tried to repro in #22032 I was unable to on master? |
@illegalnumbers : You can still add a test for this error message to close this out. |
Sounds good! I'll do so when I get a spare minute.
…On Tue, Mar 5, 2019 at 12:43 AM gfyoung ***@***.***> wrote:
@illegalnumbers <https://github.com/illegalnumbers> : You can still add a
test for this error message to close this out.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#21946 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAlwmvgso_nfq_7t3hGUUYetkIhD1168ks5vTiAKgaJpZM4VSfce>
.
|
So in creating my test for this being just about the same code in the
|
So this might need further review and debugging. |
In fact I get this:
But my local version is
|
So the issue should not be a TypeError then? Not sure what I should make this test out to be as it seems that this functionality should be expected by what is in the repo now. If you print the dataframe you get:
In the test harness. |
Not entirely sure if that's right? If we get a 0 dim array should it just be the first element? |
So I put a test for that functionality here: #25856 and I hope it gets through review. Always happy to contribute, wish I had more time! |
A 0-d array passes this check cause it is an Iterable. But later, when len() is called on, TypeError is raised.
It would be nice to implement PEP 357 to prevent this error.
The matter has been raised at PyTorch project. #9237
The text was updated successfully, but these errors were encountered: