Skip to content
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

Error from Data.second_element for some data shapes #249

Closed
davidhassell opened this issue Mar 15, 2023 · 0 comments · Fixed by #250
Closed

Error from Data.second_element for some data shapes #249

davidhassell opened this issue Mar 15, 2023 · 0 comments · Fixed by #250
Labels
bug Something isn't working
Milestone

Comments

@davidhassell
Copy link
Contributor

davidhassell commented Mar 15, 2023

For some Data shapes, the retrieval of the second element causes an error:

>>> import cfdm
>>> import numpy as np
>>> d = cfdm.Data(np.arange(6).reshape(1, 3, 2))  # This shape is OK
>>> d.array
array([[[0],
        [1],
        [2],
        [3],
        [4],
        [5]]])
>>> d.second_element()
1
>>> d = cfdm.Data(np.arange(6).reshape(1, 6, 1))  # Not OK
>>> d.second_element()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[51], line 1
----> 1 d.second_element()

File ~/cfdm/cfdm/data/data.py:3009, in Data.second_element(self)
   2981 def second_element(self):
   2982     """Return the second element of the data as a scalar.
   2983 
   2984     .. versionadded:: (cfdm) 1.7.0
   (...)
   3007 
   3008     """
-> 3009     return self._item(
   3010         (slice(0, 1, 1),) * (self.ndim - 1) + (slice(1, 2, 1),)
   3011     )

File ~/cfdm/cfdm/data/data.py:655, in Data._item(self, index)
    652 array = self[index].array
    654 if not numpy.ma.isMA(array):
--> 655     return array.item()
    657 mask = array.mask
    658 if mask is numpy.ma.nomask or not mask.item():

ValueError: can only convert an array of size 1 to a Python scalar
>>> cfdm.environment(paths=False)
Platform: Linux-5.15.0-67-generic-x86_64-with-glibc2.35
HDF5 library: 1.12.1
netcdf library: 4.8.1
Python: 3.10.9
netCDF4: 1.6.0
numpy: 1.22.3
cfdm.core: 1.10.0.3
cftime: 1.6.2
netcdf_flattener: 1.2.0
cfdm: 1.10.0.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant