Skip to content

Commit 23dca4a

Browse files
committed
TST: added testing and error messages for passing datetimes with timezones
(not yet implemented, and error message was a little misleading) CLN: better error messages on invalid appends
1 parent de3492f commit 23dca4a

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

pandas/io/pytables.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,13 +1207,20 @@ def set_atom(self, block, existing_col, min_itemsize, nan_rep, **kwargs):
12071207

12081208
self.values = list(block.items)
12091209
dtype = block.dtype.name
1210-
inferred_type = lib.infer_dtype(block.values.ravel())
1210+
rvalues = block.values.ravel()
1211+
inferred_type = lib.infer_dtype(rvalues)
12111212

12121213
if inferred_type == 'datetime64':
12131214
self.set_atom_datetime64(block)
12141215
elif inferred_type == 'date':
12151216
raise TypeError(
12161217
"[date] is not implemented as a table column")
1218+
elif inferred_type == 'datetime':
1219+
if getattr(rvalues[0],'tzinfo',None) is not None:
1220+
raise TypeError(
1221+
"timezone support on datetimes is not yet implemented as a table column")
1222+
raise TypeError(
1223+
"[datetime] is not implemented as a table column")
12171224
elif inferred_type == 'unicode':
12181225
raise TypeError(
12191226
"[unicode] is not implemented as a table column")
@@ -2080,8 +2087,18 @@ def validate(self, other):
20802087
(other.table_type, self.table_type))
20812088

20822089
for c in ['index_axes','non_index_axes','values_axes']:
2083-
if getattr(self,c,None) != getattr(other,c,None):
2084-
raise ValueError("invalid combinate of [%s] on appending data [%s] vs current table [%s]" % (c,getattr(self,c,None),getattr(other,c,None)))
2090+
sv = getattr(self,c,None)
2091+
ov = getattr(other,c,None)
2092+
if sv != ov:
2093+
2094+
# show the error for the specific axes
2095+
for i, sax in enumerate(sv):
2096+
oax = ov[i]
2097+
if sax != oax:
2098+
raise ValueError("invalid combinate of [%s] on appending data [%s] vs current table [%s]" % (c,sax,oax))
2099+
2100+
# should never get here
2101+
raise Exception("invalid combinate of [%s] on appending data [%s] vs current table [%s]" % (c,sv,ov))
20852102

20862103
@property
20872104
def nrows_expected(self):

pandas/io/tests/test_pytables.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1150,15 +1150,19 @@ def test_table_values_dtypes_roundtrip(self):
11501150
df1['float322'] = 1.
11511151
df1['float322'] = df1['float322'].astype('float32')
11521152
df1['bool'] = df1['float32'] > 0
1153+
df1['time1'] = Timestamp('20130101')
1154+
df1['time2'] = Timestamp('20130102')
11531155

11541156
store.append('df_mixed_dtypes1', df1)
11551157
result = store.select('df_mixed_dtypes1').get_dtype_counts()
11561158
expected = Series({ 'float32' : 2, 'float64' : 1,'int32' : 1, 'bool' : 1,
1157-
'int16' : 1, 'int8' : 1, 'int64' : 1, 'object' : 1 })
1159+
'int16' : 1, 'int8' : 1, 'int64' : 1, 'object' : 1,
1160+
'datetime64[ns]' : 2})
11581161
result.sort()
11591162
expected.sort()
11601163
tm.assert_series_equal(result,expected)
11611164

1165+
11621166
def test_table_mixed_dtypes(self):
11631167

11641168
# frame
@@ -1231,6 +1235,17 @@ def test_unimplemented_dtypes_table_columns(self):
12311235
# this fails because we have a date in the object block......
12321236
self.assertRaises(TypeError, store.append, 'df_unimplemented', df)
12331237

1238+
def test_table_append_with_timezones(self):
1239+
# not implemented yet
1240+
1241+
with ensure_clean(self.path) as store:
1242+
1243+
# check with mixed dtypes
1244+
df = DataFrame(dict(A = Timestamp('20130102',tz='US/Eastern')),index=range(5))
1245+
1246+
# timezones not yet supported
1247+
self.assertRaises(TypeError, store.append, 'df_tz', df)
1248+
12341249
def test_remove(self):
12351250

12361251
with ensure_clean(self.path) as store:

0 commit comments

Comments
 (0)