Skip to content

Commit

Permalink
Fixed a bug that occurred when pandas DataFrame included a datetime c…
Browse files Browse the repository at this point in the history
…olumn
  • Loading branch information
astrofrog committed Jan 11, 2019
1 parent 6e56413 commit 59aef57
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ v0.15.0 (unreleased)
v0.14.2 (unreleased)
--------------------

* Fix bug that caused date/time columns in Excel files to not be
read in correctly.

* Fix bug that caused demo VO Table to not be read in correctly with
recent versions of Numpy and Astropy. [#1911]

Expand Down
4 changes: 3 additions & 1 deletion glue/core/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


__all__ = ['Component', 'DerivedComponent', 'CategoricalComponent',
'CoordinateComponent']
'CoordinateComponent', 'DateTimeComponent']


class Component(object):
Expand Down Expand Up @@ -43,6 +43,8 @@ def __init__(self, data, units=None):
# The actual data
# subclasses may pass non-arrays here as placeholders.
if isinstance(data, np.ndarray):
if data.dtype.kind == 'M':
raise TypeError('DateTimeComponent should be used instead of Component for np.datetime64 arrays')
data = coerce_numeric(data)
data.setflags(write=False) # data is read-only

Expand Down
2 changes: 1 addition & 1 deletion glue/core/data_factories/pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def panda_process(indf):
# play well with np.unique
c = CategoricalComponent(column.fillna(''))
else:
c = Component(column.values)
c = Component.autotyped(column.values)

# convert header to string - in some cases if the first row contains
# numbers, these are cast to numerical types, so we want to change that
Expand Down
Binary file added glue/core/data_factories/tests/data/datetime.xlsx
Binary file not shown.
21 changes: 20 additions & 1 deletion glue/core/data_factories/tests/test_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import os

from numpy.testing import assert_array_equal
import numpy as np
from numpy.testing import assert_array_equal, assert_allclose

from glue.core import data_factories as df
from glue.tests.helpers import requires_xlrd, make_file
Expand Down Expand Up @@ -52,3 +53,21 @@ def test_excel_single():
assert_array_equal(d['a'], ['b', 'c', 'd', 'e'])

assert d.label == 'simple_data:Data2'


@requires_xlrd
def test_excel_datetime():

from ..excel import panda_read_excel

d = panda_read_excel(os.path.join(DATA, 'datetime.xlsx'))[0]

assert d.get_kind('date') == 'datetime'
assert d.get_kind('a') == 'numerical'
assert d.get_kind('b') == 'numerical'

expected = np.array(['2019-01-01', '2019-02-01', '2019-03-01', '2019-04-01'], dtype='datetime64[ns]')

assert_array_equal(d['date'], expected)
assert_allclose(d['a'], [61.35, 44.06, 83.02, 66.15])
assert_allclose(d['b'], [79.34, 15.66, 84.30, 61.53])

0 comments on commit 59aef57

Please sign in to comment.