Ohm
Ohm

Reputation: 2442

ValueError when trying to create Table object in Astropy

I am trying to construct a Table object from astropy.table, for now I wish to add only one column, but I am getting a ValueError.

Does anyone know what I am doing wrong?

>>> br_data["mass"]
array([   49.65092267,   269.50829639,    51.37768973, ...,  1168.74318299,
        1144.96728692,  1116.72595158])
>>> len(br_data["mass"])
122911
>>> table = Table([br_data["mass"]], names=('mDM'), meta={'name': 'attempt'})
ERROR: ValueError: Arguments "names" and "dtype" must match number of columns [astropy.table.table]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/astropy/table/table.py", line 1114, in __init__
    self._check_names_dtype(names, dtype, n_cols)
  File "/usr/local/lib/python2.7/dist-packages/astropy/table/table.py", line 1207, in _check_names_dtype
    .format(inp_str))
ValueError: Arguments "names" and "dtype" must match number of columns
>>> 
>>> br_data["mass"].dtype
dtype('float64')
>>> br_data["mass"].shape
(122911,)

Upvotes: 0

Views: 2231

Answers (1)

Iguananaut
Iguananaut

Reputation: 23366

The names argument should be a collection of name strings of length equal to the number of columns. You wrote names=('mDM') which in Python is equivalent to names='mDM' (the parentheses are ignored).

I think what you intended was a one element tuple, which in Python is written ('mDM',) (note the comma). This is to prevent ambiguity with parentheses used to group expressions. Or you can just use a list: names=['mDM'].

Upvotes: 1

Related Questions