本文整理汇总了Python中pandas.util.testing.assert_dict_equal方法的典型用法代码示例。如果您正苦于以下问题:Python testing.assert_dict_equal方法的具体用法?Python testing.assert_dict_equal怎么用?Python testing.assert_dict_equal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas.util.testing
的用法示例。
在下文中一共展示了testing.assert_dict_equal方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_index_groupby
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_dict_equal [as 别名]
def test_index_groupby(self):
int_idx = Index(range(6))
float_idx = Index(np.arange(0, 0.6, 0.1))
obj_idx = Index('A B C D E F'.split())
dt_idx = pd.date_range('2013-01-01', freq='M', periods=6)
for idx in [int_idx, float_idx, obj_idx, dt_idx]:
to_groupby = np.array([1, 2, np.nan, np.nan, 2, 1])
tm.assert_dict_equal(idx.groupby(to_groupby),
{1.0: idx[[0, 5]], 2.0: idx[[1, 4]]})
to_groupby = Index([datetime(2011, 11, 1),
datetime(2011, 12, 1),
pd.NaT,
pd.NaT,
datetime(2011, 12, 1),
datetime(2011, 11, 1)],
tz='UTC').values
ex_keys = [Timestamp('2011-11-01'), Timestamp('2011-12-01')]
expected = {ex_keys[0]: idx[[0, 5]],
ex_keys[1]: idx[[1, 4]]}
tm.assert_dict_equal(idx.groupby(to_groupby), expected)
示例2: test_observed_groups
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_dict_equal [as 别名]
def test_observed_groups(observed):
# gh-20583
# test that we have the appropriate groups
cat = pd.Categorical(['a', 'c', 'a'], categories=['a', 'b', 'c'])
df = pd.DataFrame({'cat': cat, 'vals': [1, 2, 3]})
g = df.groupby('cat', observed=observed)
result = g.groups
if observed:
expected = {'a': Index([0, 2], dtype='int64'),
'c': Index([1], dtype='int64')}
else:
expected = {'a': Index([0, 2], dtype='int64'),
'b': Index([], dtype='int64'),
'c': Index([1], dtype='int64')}
tm.assert_dict_equal(result, expected)
示例3: test_multiindex_columns_empty_level
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_dict_equal [as 别名]
def test_multiindex_columns_empty_level(self):
lst = [['count', 'values'], ['to filter', '']]
midx = MultiIndex.from_tuples(lst)
df = DataFrame([[long(1), 'A']], columns=midx)
grouped = df.groupby('to filter').groups
assert grouped['A'] == [0]
grouped = df.groupby([('to filter', '')]).groups
assert grouped['A'] == [0]
df = DataFrame([[long(1), 'A'], [long(2), 'B']], columns=midx)
expected = df.groupby('to filter').groups
result = df.groupby([('to filter', '')]).groups
assert result == expected
df = DataFrame([[long(1), 'A'], [long(2), 'A']], columns=midx)
expected = df.groupby('to filter').groups
result = df.groupby([('to filter', '')]).groups
tm.assert_dict_equal(result, expected)
示例4: test_groupby_multiindex_tuple
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_dict_equal [as 别名]
def test_groupby_multiindex_tuple(self):
# GH 17979
df = pd.DataFrame([[1, 2, 3, 4], [3, 4, 5, 6], [1, 4, 2, 3]],
columns=pd.MultiIndex.from_arrays(
[['a', 'b', 'b', 'c'],
[1, 1, 2, 2]]))
expected = df.groupby([('b', 1)]).groups
result = df.groupby(('b', 1)).groups
tm.assert_dict_equal(expected, result)
df2 = pd.DataFrame(df.values,
columns=pd.MultiIndex.from_arrays(
[['a', 'b', 'b', 'c'],
['d', 'd', 'e', 'e']]))
expected = df2.groupby([('b', 'd')]).groups
result = df.groupby(('b', 1)).groups
tm.assert_dict_equal(expected, result)
df3 = pd.DataFrame(df.values,
columns=[('a', 'd'), ('b', 'd'), ('b', 'e'), 'c'])
expected = df3.groupby([('b', 'd')]).groups
result = df.groupby(('b', 1)).groups
tm.assert_dict_equal(expected, result)
示例5: test_list_grouper_with_nat
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_dict_equal [as 别名]
def test_list_grouper_with_nat(self):
# GH 14715
df = pd.DataFrame({'date': pd.date_range('1/1/2011',
periods=365, freq='D')})
df.iloc[-1] = pd.NaT
grouper = pd.Grouper(key='date', freq='AS')
# Grouper in a list grouping
result = df.groupby([grouper])
expected = {pd.Timestamp('2011-01-01'): pd.Index(list(range(364)))}
tm.assert_dict_equal(result.groups, expected)
# Test case without a list
result = df.groupby(grouper)
expected = {pd.Timestamp('2011-01-01'): 365}
tm.assert_dict_equal(result.groups, expected)
# get_group
# --------------------------------
示例6: test_groupby
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_dict_equal [as 别名]
def test_groupby(self):
index = Index(range(5))
result = index.groupby(np.array([1, 1, 2, 2, 2]))
expected = {1: pd.Index([0, 1]), 2: pd.Index([2, 3, 4])}
tm.assert_dict_equal(result, expected)
示例7: test_groupby
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_dict_equal [as 别名]
def test_groupby(idx):
groups = idx.groupby(np.array([1, 1, 1, 2, 2, 2]))
labels = idx.get_values().tolist()
exp = {1: labels[:3], 2: labels[3:]}
tm.assert_dict_equal(groups, exp)
# GH5620
groups = idx.groupby(idx)
exp = {key: [key] for key in idx}
tm.assert_dict_equal(groups, exp)
示例8: test_pickle_v0_15_2
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_dict_equal [as 别名]
def test_pickle_v0_15_2(self, datapath):
offsets = {'DateOffset': DateOffset(years=1),
'MonthBegin': MonthBegin(1),
'Day': Day(1),
'YearBegin': YearBegin(1),
'Week': Week(1)}
pickle_path = datapath('tseries', 'offsets', 'data',
'dateoffset_0_15_2.pickle')
# This code was executed once on v0.15.2 to generate the pickle:
# with open(pickle_path, 'wb') as f: pickle.dump(offsets, f)
#
tm.assert_dict_equal(offsets, read_pickle(pickle_path))
示例9: test_query_inplace
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_dict_equal [as 别名]
def test_query_inplace(self):
# see gh-11149
df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})
expected = df.copy()
expected = expected[expected['a'] == 2]
df.query('a == 2', inplace=True)
assert_frame_equal(expected, df)
df = {}
expected = {"a": 3}
self.eval("a = 1 + 2", target=df, inplace=True)
tm.assert_dict_equal(df, expected)
示例10: test_observed_groups_with_nan
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_dict_equal [as 别名]
def test_observed_groups_with_nan(observed):
# GH 24740
df = pd.DataFrame({'cat': pd.Categorical(['a', np.nan, 'a'],
categories=['a', 'b', 'd']),
'vals': [1, 2, 3]})
g = df.groupby('cat', observed=observed)
result = g.groups
if observed:
expected = {'a': Index([0, 2], dtype='int64')}
else:
expected = {'a': Index([0, 2], dtype='int64'),
'b': Index([], dtype='int64'),
'd': Index([], dtype='int64')}
tm.assert_dict_equal(result, expected)
示例11: test_frame_to_dict_tz
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_dict_equal [as 别名]
def test_frame_to_dict_tz(self):
# GH18372 When converting to dict with orient='records' columns of
# datetime that are tz-aware were not converted to required arrays
data = [(datetime(2017, 11, 18, 21, 53, 0, 219225, tzinfo=pytz.utc),),
(datetime(2017, 11, 18, 22, 6, 30, 61810, tzinfo=pytz.utc,),)]
df = DataFrame(list(data), columns=["d", ])
result = df.to_dict(orient='records')
expected = [
{'d': Timestamp('2017-11-18 21:53:00.219225+0000', tz=pytz.utc)},
{'d': Timestamp('2017-11-18 22:06:30.061810+0000', tz=pytz.utc)},
]
tm.assert_dict_equal(result[0], expected[0])
tm.assert_dict_equal(result[1], expected[1])
示例12: test_read_dta18
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_dict_equal [as 别名]
def test_read_dta18(self):
parsed_118 = self.read_dta(self.dta22_118)
parsed_118["Bytes"] = parsed_118["Bytes"].astype('O')
expected = DataFrame.from_records(
[['Cat', 'Bogota', u'Bogotá', 1, 1.0, u'option b Ünicode', 1.0],
['Dog', 'Boston', u'Uzunköprü', np.nan, np.nan, np.nan, np.nan],
['Plane', 'Rome', u'Tromsø', 0, 0.0, 'option a', 0.0],
['Potato', 'Tokyo', u'Elâzığ', -4, 4.0, 4, 4],
['', '', '', 0, 0.3332999, 'option a', 1 / 3.]
],
columns=['Things', 'Cities', 'Unicode_Cities_Strl',
'Ints', 'Floats', 'Bytes', 'Longs'])
expected["Floats"] = expected["Floats"].astype(np.float32)
for col in parsed_118.columns:
tm.assert_almost_equal(parsed_118[col], expected[col])
with StataReader(self.dta22_118) as rdr:
vl = rdr.variable_labels()
vl_expected = {u'Unicode_Cities_Strl':
u'Here are some strls with Ünicode chars',
u'Longs': u'long data',
u'Things': u'Here are some things',
u'Bytes': u'byte data',
u'Ints': u'int data',
u'Cities': u'Here are some cities',
u'Floats': u'float data'}
tm.assert_dict_equal(vl, vl_expected)
assert rdr.data_label == u'This is a Ünicode data label'
示例13: test_get_level_lengths
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_dict_equal [as 别名]
def test_get_level_lengths(self):
index = pd.MultiIndex.from_product([['a', 'b'], [0, 1, 2]])
expected = {(0, 0): 3, (0, 3): 3, (1, 0): 1, (1, 1): 1, (1, 2): 1,
(1, 3): 1, (1, 4): 1, (1, 5): 1}
result = _get_level_lengths(index)
tm.assert_dict_equal(result, expected)
示例14: test_get_level_lengths_un_sorted
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_dict_equal [as 别名]
def test_get_level_lengths_un_sorted(self):
index = pd.MultiIndex.from_arrays([
[1, 1, 2, 1],
['a', 'b', 'b', 'd']
])
expected = {(0, 0): 2, (0, 2): 1, (0, 3): 1,
(1, 0): 1, (1, 1): 1, (1, 2): 1, (1, 3): 1}
result = _get_level_lengths(index)
tm.assert_dict_equal(result, expected)
示例15: test_na_values_dict_aliasing
# 需要导入模块: from pandas.util import testing [as 别名]
# 或者: from pandas.util.testing import assert_dict_equal [as 别名]
def test_na_values_dict_aliasing(all_parsers):
parser = all_parsers
na_values = {"a": 2, "b": 1}
na_values_copy = na_values.copy()
names = ["a", "b"]
data = "1,2\n2,1"
expected = DataFrame([[1.0, 2.0], [np.nan, np.nan]], columns=names)
result = parser.read_csv(StringIO(data), names=names, na_values=na_values)
tm.assert_frame_equal(result, expected)
tm.assert_dict_equal(na_values, na_values_copy)