Skip to content
This repository has been archived by the owner on Apr 21, 2020. It is now read-only.

Commit

Permalink
Allow time_col type to be string
Browse files Browse the repository at this point in the history
  • Loading branch information
Keisuke Nishida committed Jun 6, 2015
1 parent ed9c4b2 commit b7bda91
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
16 changes: 7 additions & 9 deletions pandas_td/td.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ def to_td(frame, name, con, if_exists='fail', time_col=None, time_index=None, in
- append: If table exists, insert data. Create if does not exist.
time_col : string, optional
Column name to use as "time" column for the table. Column type must be
integer (unixtime) or datetime. If None is given (default), then the current
time is used as time values.
integer (unixtime), datetime, or string. If None is given (default),
then the current time is used as time values.
time_index : int, optional
Level of index to use as "time" column for the table. Set 0 for a single index.
This parameter implies index=False.
Expand Down Expand Up @@ -392,15 +392,13 @@ def _convert_dataframe(frame, time_col=None, time_index=None, index=None, index_
if 'time' in frame.columns and time_col != 'time':
raise ValueError('"time" column already exists')
if time_col is not None:
col = frame[time_col]
if col.dtype not in (np.dtype('int64'), np.dtype('datetime64[ns]')):
raise TypeError('time type must be either int64 or datetime64')
if time_col != 'time':
frame.rename(columns={time_col: 'time'}, inplace=True)
col = frame['time']
if col.dtype != np.dtype('int64'):
if col.dtype != np.dtype('datetime64[ns]'):
col = pd.to_datetime(col)
frame['time'] = col.astype(np.int64, copy=True) // (10 ** 9)
if time_col != 'time':
frame.drop(time_col, axis=1, inplace=True)
elif time_col != 'time':
frame.rename(columns={time_col: 'time'}, inplace=True)
elif time_index is not None:
if type(time_index) is bool or not isinstance(time_index, six.integer_types):
raise TypeError('invalid type for time_index')
Expand Down
16 changes: 8 additions & 8 deletions pandas_td/test_td.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,26 +399,26 @@ def test_time_now(self):
f2 = _convert_dataframe(f1)
eq_(list(f2.columns), ['x', 'y', 'time'])

def test_time_col_rename(self):
f1 = pd.DataFrame([[0, 'a', 1], [0, 'b', 2]], columns=['unixtime', 'x', 'y'])
f2 = _convert_dataframe(f1, time_col='unixtime')
eq_(list(f2.columns), ['time', 'x', 'y'])

def test_time_col_by_unixtime(self):
f1 = pd.DataFrame([[0, 'a', 1], [0, 'b', 2]], columns=['time', 'x', 'y'])
f2 = _convert_dataframe(f1, time_col='time')
eq_(list(f2.columns), ['time', 'x', 'y'])

def test_time_col_by_unixtime_rename(self):
f1 = pd.DataFrame([[0, 'a', 1], [0, 'b', 2]], columns=['unixtime', 'x', 'y'])
f2 = _convert_dataframe(f1, time_col='unixtime')
eq_(list(f2.columns), ['time', 'x', 'y'])

def test_time_col_by_datetime(self):
f1 = pd.DataFrame([['a', 1], ['b', 2]], columns=['x', 'y'])
f1['time'] = pd.to_datetime('2001-01-01')
f2 = _convert_dataframe(f1, time_col='time')
eq_(list(f2.columns), ['x', 'y', 'time'])

def test_time_col_by_datetime_rename(self):
def test_time_col_by_string(self):
f1 = pd.DataFrame([['a', 1], ['b', 2]], columns=['x', 'y'])
f1['date'] = pd.to_datetime('2001-01-01')
f2 = _convert_dataframe(f1, time_col='date')
f1['time'] = '2001-01-01'
f2 = _convert_dataframe(f1, time_col='time')
eq_(list(f2.columns), ['x', 'y', 'time'])

@raises(TypeError)
Expand Down

0 comments on commit b7bda91

Please sign in to comment.