def test_to_sql_index_label(self): temp_frame = DataFrame({'col1': range(4)}) # no index name, defaults to 'pandas_index' sql.to_sql(temp_frame, 'test_index_label', self.conn) frame = sql.read_table('test_index_label', self.conn) self.assertEqual(frame.columns[0], 'pandas_index') # specifying index_label sql.to_sql(temp_frame, 'test_index_label', self.conn, if_exists='replace', index_label='other_label') frame = sql.read_table('test_index_label', self.conn) self.assertEqual(frame.columns[0], 'other_label', "Specified index_label not written to database") # using the index name temp_frame.index.name = 'index' sql.to_sql(temp_frame, 'test_index_label', self.conn, if_exists='replace') frame = sql.read_table('test_index_label', self.conn) self.assertEqual(frame.columns[0], 'index', "Index name not written to database") # has index name, but specifying index_label sql.to_sql(temp_frame, 'test_index_label', self.conn, if_exists='replace', index_label='other_label') frame = sql.read_table('test_index_label', self.conn) self.assertEqual(frame.columns[0], 'other_label', "Specified index_label not written to database")
def test_default_date_load(self): df = sql.read_table("types_test_data", self.conn) # IMPORTANT - sqlite has no native date type, so shouldn't parse, # but MySQL SHOULD be converted. self.assertTrue( issubclass(df.DateCol.dtype.type, np.datetime64), "DateCol loaded with incorrect type")
def test_default_date_load(self): df = sql.read_table("types_test_data", self.conn) # IMPORTANT - sqlite has no native date type, so shouldn't parse, but # MySQL SHOULD be converted. self.assertTrue(issubclass(df.DateCol.dtype.type, np.datetime64), "DateCol loaded with incorrect type")
def test_read_table_columns(self): # test columns argument in read_table sql.to_sql(self.test_frame1, 'test_frame', self.conn) cols = ['A', 'B'] result = sql.read_table('test_frame', self.conn, columns=cols) self.assertEqual(result.columns.tolist(), cols, "Columns not correctly selected")
def test_date_parsing(self): """ Test date parsing """ # No Parsing df = sql.read_table("types_test_data", self.conn) df = sql.read_table( "types_test_data", self.conn, parse_dates=['DateCol']) self.assertTrue( issubclass(df.DateCol.dtype.type, np.datetime64), "DateCol loaded with incorrect type") df = sql.read_table( "types_test_data", self.conn, parse_dates={'DateCol': '%Y-%m-%d %H:%M:%S'}) self.assertTrue( issubclass(df.DateCol.dtype.type, np.datetime64), "DateCol loaded with incorrect type") df = sql.read_table("types_test_data", self.conn, parse_dates={ 'DateCol': {'format': '%Y-%m-%d %H:%M:%S'}}) self.assertTrue(issubclass(df.DateCol.dtype.type, np.datetime64), "IntDateCol loaded with incorrect type") df = sql.read_table( "types_test_data", self.conn, parse_dates=['IntDateCol']) self.assertTrue(issubclass(df.IntDateCol.dtype.type, np.datetime64), "IntDateCol loaded with incorrect type") df = sql.read_table( "types_test_data", self.conn, parse_dates={'IntDateCol': 's'}) self.assertTrue(issubclass(df.IntDateCol.dtype.type, np.datetime64), "IntDateCol loaded with incorrect type") df = sql.read_table( "types_test_data", self.conn, parse_dates={'IntDateCol': {'unit': 's'}}) self.assertTrue(issubclass(df.IntDateCol.dtype.type, np.datetime64), "IntDateCol loaded with incorrect type")
def test_read_table_index_col(self): # test columns argument in read_table sql.to_sql(self.test_frame1, 'test_frame', self.conn) result = sql.read_table('test_frame', self.conn, index_col="index") self.assertEqual(result.index.names, ["index"], "index_col not correctly set") result = sql.read_table('test_frame', self.conn, index_col=["A", "B"]) self.assertEqual(result.index.names, ["A", "B"], "index_col not correctly set") result = sql.read_table('test_frame', self.conn, index_col=["A", "B"], columns=["C", "D"]) self.assertEqual(result.index.names, ["A", "B"], "index_col not correctly set") self.assertEqual(result.columns.tolist(), ["C", "D"], "columns not set correctly whith index_col")
def test_mixed_dtype_insert(self): # see GH6509 s1 = Series(2**25 + 1,dtype=np.int32) s2 = Series(0.0,dtype=np.float32) df = DataFrame({'s1': s1, 's2': s2}) # write and read again df.to_sql("test_read_write", self.conn, index=False) df2 = sql.read_table("test_read_write", self.conn) tm.assert_frame_equal(df, df2, check_dtype=False, check_exact=True)
def test_mixed_dtype_insert(self): # see GH6509 s1 = Series(2**25 + 1, dtype=np.int32) s2 = Series(0.0, dtype=np.float32) df = DataFrame({'s1': s1, 's2': s2}) # write and read again df.to_sql("test_read_write", self.conn, index=False) df2 = sql.read_table("test_read_write", self.conn) tm.assert_frame_equal(df, df2, check_dtype=False, check_exact=True)
def test_to_sql_index_label_multiindex(self): temp_frame = DataFrame({'col1': range(4)}, index=MultiIndex.from_product([('A0', 'A1'), ('B0', 'B1')])) # no index name, defaults to 'level_0' and 'level_1' sql.to_sql(temp_frame, 'test_index_label', self.conn) frame = sql.read_table('test_index_label', self.conn) self.assertEqual(frame.columns[0], 'level_0') self.assertEqual(frame.columns[1], 'level_1') # specifying index_label sql.to_sql(temp_frame, 'test_index_label', self.conn, if_exists='replace', index_label=['A', 'B']) frame = sql.read_table('test_index_label', self.conn) self.assertEqual(frame.columns[:2].tolist(), ['A', 'B'], "Specified index_labels not written to database") # using the index name temp_frame.index.names = ['A', 'B'] sql.to_sql(temp_frame, 'test_index_label', self.conn, if_exists='replace') frame = sql.read_table('test_index_label', self.conn) self.assertEqual(frame.columns[:2].tolist(), ['A', 'B'], "Index names not written to database") # has index name, but specifying index_label sql.to_sql(temp_frame, 'test_index_label', self.conn, if_exists='replace', index_label=['C', 'D']) frame = sql.read_table('test_index_label', self.conn) self.assertEqual(frame.columns[:2].tolist(), ['C', 'D'], "Specified index_labels not written to database") # wrong length of index_label self.assertRaises(ValueError, sql.to_sql, temp_frame, 'test_index_label', self.conn, if_exists='replace', index_label='C')
def test_default_type_convertion(self): """ Test default type conversion""" df = sql.read_table("types_test_data", self.conn) self.assertTrue( issubclass(df.FloatCol.dtype.type, np.floating), "FloatCol loaded with incorrect type") self.assertTrue( issubclass(df.IntCol.dtype.type, np.integer), "IntCol loaded with incorrect type") self.assertTrue( issubclass(df.BoolCol.dtype.type, np.integer), "BoolCol loaded with incorrect type") # Int column with NA values stays as float self.assertTrue(issubclass(df.IntColWithNull.dtype.type, np.floating), "IntColWithNull loaded with incorrect type") # Non-native Bool column with NA values stays as float self.assertTrue( issubclass(df.BoolColWithNull.dtype.type, np.floating), "BoolCol loaded with incorrect type")
def test_default_type_convertion(self): df = sql.read_table("types_test_data", self.conn) self.assertTrue(issubclass(df.FloatCol.dtype.type, np.floating), "FloatCol loaded with incorrect type") self.assertTrue(issubclass(df.IntCol.dtype.type, np.integer), "IntCol loaded with incorrect type") self.assertTrue(issubclass(df.BoolCol.dtype.type, np.bool_), "BoolCol loaded with incorrect type") # Int column with NA values stays as float self.assertTrue(issubclass(df.IntColWithNull.dtype.type, np.floating), "IntColWithNull loaded with incorrect type") # Bool column with NA values becomes object self.assertTrue(issubclass(df.BoolColWithNull.dtype.type, np.object), "BoolColWithNull loaded with incorrect type")
def test_default_type_conversion(self): df = sql.read_table("types_test_data", self.conn) self.assertTrue(issubclass(df.FloatCol.dtype.type, np.floating), "FloatCol loaded with incorrect type") self.assertTrue(issubclass(df.IntCol.dtype.type, np.integer), "IntCol loaded with incorrect type") # MySQL has no real BOOL type (it's an alias for TINYINT) self.assertTrue(issubclass(df.BoolCol.dtype.type, np.integer), "BoolCol loaded with incorrect type") # Int column with NA values stays as float self.assertTrue(issubclass(df.IntColWithNull.dtype.type, np.floating), "IntColWithNull loaded with incorrect type") # Bool column with NA = int column with NA values => becomes float self.assertTrue(issubclass(df.BoolColWithNull.dtype.type, np.floating), "BoolColWithNull loaded with incorrect type")
def test_read_table(self): iris_frame = sql.read_table("iris", con=self.conn) self._check_iris_loaded_frame(iris_frame)
def test_read_table_columns(self): iris_frame = sql.read_table("iris", con=self.conn, columns=['SepalLength', 'SepalLength']) tm.equalContents(iris_frame.columns.values, ['SepalLength', 'SepalLength'])
def test_read_table_columns(self): iris_frame = sql.read_table( "iris", con=self.conn, columns=['SepalLength', 'SepalLength']) tm.equalContents( iris_frame.columns.values, ['SepalLength', 'SepalLength'])