question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

QTable dtype not preserved on quantity columns

See original GitHub issue

If I attribute a column with a unit, than the datatype of the column is lost. I’ve tried on Astropy 3.0.8, 2.0.3, and 1.2.1. On astropy 1.2.1 (you probably don’t care), I cannot pull the astype trick to fix the dtype afterwards, with the error “Value not scalar compatible or convertible to an int, float or complex array”

import astropy
import astropy.table
import numpy
import sys

print('Python version: '+sys.version)
print('Astropy version: '+astropy.version.version)
print('Numpy version: '+numpy.version.version)

a = astropy.table.QTable()
b = astropy.table.QTable.Column(data=numpy.zeros(1,dtype=numpy.uint8),name='Crazy',dtype=numpy.uint8,unit='m')
a.add_column(b)
# I would expect the dtype to be uint8
# If I do not set the unit above, it will be
print(repr(a))

a['Crazy'] = a['Crazy'].astype(numpy.uint8)
print(repr(a))

I’ve tried it on two setups

Python version: 2.7.15+ (default, Oct  2 2018, 22:12:08)
[GCC 8.2.0]
Astropy version: 2.0.8
Numpy version: 1.15.0
<QTable length=1>
 Crazy
   m
float64
-------
    0.0
<QTable length=1>
Crazy
  m
uint8
-----
    0
Python version: 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0]
Astropy version: 3.0.3
Numpy version: 1.15.0
<QTable length=1>
 Crazy
   m
float64
-------
    0.0
<QTable length=1>
Crazy
  m
uint8
-----
    0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mhvkcommented, Jan 8, 2019

Indeed, by default Quantity casts all input to float (since this makes most sense for anything with units), and thus happens when you add a Column item to your table. If you want to override this, you can do:

a['Crazy'] = astropy.units.Quantity(numpy.zeros(1,dtype=numpy.uint8), dtype=numpy.uint8)

Or, equivalently,

b = astropy.units.Quantity(numpy.zeros(1,dtype=numpy.uint8), dtype=numpy.uint8)
a.add_column(b, name='Crazy')
0reactions
astropy-bot[bot]commented, Apr 25, 2019

I’m going to close this issue as per my previous message, but if you feel that this issue should stay open, then feel free to re-open and remove the Close? label.

If this is the first time I am commenting on this issue, or if you believe I closed this issue incorrectly, please report this here

Read more comments on GitHub >

github_iconTop Results From Across the Web

QTable ignores dtype, tries converting string column with unit ...
Description I have a table that (ab)uses the "unit" metadata for some additional info on non-numeric strings, mostly by putting the String ...
Read more >
Data Tables (astropy.table) — Astropy v5.2
Column d is a Quantity array. Since we used QTable , this stores a native Quantity within the table and brings the full...
Read more >
Mixin columns — Astropy v1.0.4
These mixin column objects are not converted in any way but are used natively. ... QTable is exactly the same as Table except...
Read more >
Saving updated dataframe from edited PyQt5 QTableView object
Columns that are specified as "Default" are kept with the original column name. Example of the Code below: form_class = uic.loadUiType(" ...
Read more >
Data Tables (astropy.table) — Astropy v1.2.dev14793
Instead the table is stored as a collection of individual column objects. ... see Quantity and QTable for a way to natively use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found