Skip to content

Cannot sort table by Time column #10823

Closed

Description

@pllim

Description

I have a Table with a column containing Time objects. I want to sort the rows by that column with Time objects. I get AttributeError: 'Time' object has no attribute 'view' error. Same error using QTable.

I found #8068 but in that issue, the sort wasn't done with the Time column even though the table contains a Time column.

Expected behavior

Table successfully sorted in-place by ascending Time order.

Actual behavior

Unsuccessful operation with AttributeError: 'Time' object has no attribute 'view' error.

Steps to Reproduce

>>> from astropy.table import Table
>>> from astropy.time import Time
>>> myrows = [('first row', Time('2001-01-01')), ('second row', Time('1980-12-31'))]
>>> tab = Table(rows=myrows, names=('a', 'b'))
>>> tab.sort('a')  # No error
>>> tab.sort('b')  # Traceback below
.../astropy/table/table.py in sort(self, keys, reverse)
   2960             keys = [keys]
   2961 
-> 2962         indexes = self.argsort(keys)
   2963 
   2964         if reverse:

.../astropy/table/table.py in argsort(self, keys, kind, reverse)
   2892                 data = self.as_array(names=keys)
   2893             else:
-> 2894                 data = self[keys[0]].view(np.ndarray)
   2895         else:
   2896             # No keys provided so sort on all columns.

.../astropy/time/core.py in __getattr__(self, attr)
   1341         else:
   1342             # Should raise AttributeError
-> 1343             return self.__getattribute__(attr)
   1344 
   1345     @override__dir__

AttributeError: 'Time' object has no attribute 'view'

System Details

Linux-3.10.0-1127.19.1.el7.x86_64-x86_64-with-glibc2.10
Python 3.8.3 (default, May 19 2020, 18:47:26)
[GCC 7.3.0]
Numpy 1.19.1
astropy 4.2.dev687+g214a599c1
Scipy 1.5.1
Matplotlib 3.3.1

Activity

  1. pllim commented on Oct 9, 2020

    @pllim
    MemberAuthor

    Workaround is to do Time(...).strftime('%Y-%m-%d') and then sort the string.

  2. mhvk commented on Oct 9, 2020

    @mhvk
    Contributor

    Hmm, this looks like a regression - this worked in 4.0! It is strange since Time does have an argsort method.

  3. mhvk commented on Oct 9, 2020

    @mhvk
    Contributor

    So another work-around is

    tab[tab['b'].argsort()]
    
  4. mhvk commented on Oct 9, 2020

    @mhvk
    Contributor

    The fix turns out to be fairly easy - PR forthcoming...

  5. mhvk commented on Oct 9, 2020

    @mhvk
    Contributor

    See #10824

  6. pllim commented on Oct 17, 2020

    @pllim
    MemberAuthor

    Thanks, all!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions