def test_autocast_mixed_types_to_str(self): # This is important in particular for Excel data, which is often a mix # of int and str. table = pd.DataFrame({"A": ["1A", 2]}) scrapetable.autocast_dtypes_in_place(table) expected = pd.DataFrame({"A": ["1A", "2"]}) assert_frame_equal(table, expected)
def test_autocast_cast_crazy_types(self): class Obj: def __init__(self, s): self.s = s def __str__(self): return self.s obj1 = Obj("o1") obj2 = Obj("o2") table = pd.DataFrame({"A": [obj1, obj2]}) scrapetable.autocast_dtypes_in_place(table) expected = pd.DataFrame({"A": ["o1", "o2"]}) assert_frame_equal(table, expected)
def test_autocast_int_from_str_categories_with_empty_str(self): table = pd.DataFrame({"A": ["", "", "1"]}, dtype="category") scrapetable.autocast_dtypes_in_place(table) expected = pd.DataFrame({"A": [np.nan, np.nan, 1.0]}, dtype=np.float64) assert_frame_equal(table, expected)
def test_autocast_str_categories_from_str_categories(self): table = pd.DataFrame({"A": ["1", "2.1", "Yay"]}, dtype="category") scrapetable.autocast_dtypes_in_place(table) # should be no-op expected = pd.DataFrame({"A": ["1", "2.1", "Yay"]}, dtype="category") assert_frame_equal(table, expected)
def test_autocast_float_from_str_categories_with_empty_str(self): # example: used read_csv(dtype='category'), now want floats table = pd.DataFrame({"A": ["1", "2.1", ""]}, dtype="category") scrapetable.autocast_dtypes_in_place(table) expected = pd.DataFrame({"A": [1.0, 2.1, np.nan]}, dtype=np.float64) assert_frame_equal(table, expected)
def test_autocast_float_from_str_categories_with_dup_floats(self): table = pd.DataFrame({"A": ["1", "1.0"]}, dtype="category") scrapetable.autocast_dtypes_in_place(table) expected = pd.DataFrame({"A": [1.0, 1.0]}, dtype=np.float64) assert_frame_equal(table, expected)
def test_autocast_int_from_str_categories(self): # example: used read_csv(dtype='category'), now want ints table = pd.DataFrame({"A": ["1", "2"]}, dtype="category") scrapetable.autocast_dtypes_in_place(table) expected = pd.DataFrame({"A": [1, 2]}) assert_frame_equal(table, expected)
def test_autocast_int_from_str(self): table = pd.DataFrame({"A": ["1", "2"]}) scrapetable.autocast_dtypes_in_place(table) expected = pd.DataFrame({"A": [1, 2]}) assert_frame_equal(table, expected)
def test_autocast_all_empty_or_null_categories_is_text(self): table = pd.DataFrame({"A": ["", np.nan, ""]}, dtype="category") scrapetable.autocast_dtypes_in_place(table) expected = pd.DataFrame({"A": ["", np.nan, ""]}, dtype="category") assert_frame_equal(table, expected)
def test_autocast_all_empty_str_is_text(self): table = pd.DataFrame({"A": ["", ""]}) scrapetable.autocast_dtypes_in_place(table) assert_frame_equal(table, pd.DataFrame({"A": ["", ""]}))
def test_autocast_all_null_is_text(self): table = pd.DataFrame({"A": [np.nan, np.nan]}, dtype=object) scrapetable.autocast_dtypes_in_place(table) expected = pd.DataFrame({"A": [np.nan, np.nan]}, dtype=object) assert_frame_equal(table, expected)