Skip to content

Commit ab0bcfc

Browse files
topper-123harisbal
authored and
harisbal
committed
Let initialisation from dicts use insertion order for python >= 3.6 (part II) (pandas-dev#19859)
1 parent f8a3e72 commit ab0bcfc

File tree

11 files changed

+86
-77
lines changed

11 files changed

+86
-77
lines changed

pandas/tests/groupby/test_groupby.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ def max_value(group):
9999

100100
applied = df.groupby('A').apply(max_value)
101101
result = applied.get_dtype_counts().sort_values()
102-
expected = Series({'object': 2,
103-
'float64': 2,
104-
'int64': 1}).sort_values()
102+
expected = Series({'float64': 2,
103+
'int64': 1,
104+
'object': 2}).sort_values()
105105
assert_series_equal(result, expected)
106106

107107
def test_groupby_return_type(self):
@@ -244,7 +244,7 @@ def func_with_no_date(batch):
244244
return pd.Series({'c': 2})
245245

246246
def func_with_date(batch):
247-
return pd.Series({'c': 2, 'b': datetime(2015, 1, 1)})
247+
return pd.Series({'b': datetime(2015, 1, 1), 'c': 2})
248248

249249
dfg_no_conversion = df.groupby(by=['a']).apply(func_with_no_date)
250250
dfg_no_conversion_expected = pd.DataFrame({'c': 2}, index=[1])
@@ -1628,8 +1628,8 @@ def f(g):
16281628

16291629
def test_apply_with_mixed_dtype(self):
16301630
# GH3480, apply with mixed dtype on axis=1 breaks in 0.11
1631-
df = DataFrame({'foo1': ['one', 'two', 'two', 'three', 'one', 'two'],
1632-
'foo2': np.random.randn(6)})
1631+
df = DataFrame({'foo1': np.random.randn(6),
1632+
'foo2': ['one', 'two', 'two', 'three', 'one', 'two']})
16331633
result = df.apply(lambda x: x, axis=1)
16341634
assert_series_equal(df.get_dtype_counts(), result.get_dtype_counts())
16351635

@@ -2113,10 +2113,10 @@ def test_multifunc_sum_bug(self):
21132113

21142114
def test_handle_dict_return_value(self):
21152115
def f(group):
2116-
return {'min': group.min(), 'max': group.max()}
2116+
return {'max': group.max(), 'min': group.min()}
21172117

21182118
def g(group):
2119-
return Series({'min': group.min(), 'max': group.max()})
2119+
return Series({'max': group.max(), 'min': group.min()})
21202120

21212121
result = self.df.groupby('A')['C'].apply(f)
21222122
expected = self.df.groupby('A')['C'].apply(g)

pandas/tests/groupby/test_transform.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,9 @@ def test_cython_transform_frame(self, op, args, targop):
519519
'timedelta': pd.timedelta_range(1, freq='s',
520520
periods=1000),
521521
'string': strings * 50,
522-
'string_missing': strings_missing * 50})
522+
'string_missing': strings_missing * 50},
523+
columns=['float', 'float_missing', 'int', 'datetime',
524+
'timedelta', 'string', 'string_missing'])
523525
df['cat'] = df['string'].astype('category')
524526

525527
df2 = df.copy()
@@ -552,7 +554,9 @@ def test_cython_transform_frame(self, op, args, targop):
552554
tm.assert_frame_equal(expected,
553555
gb.transform(op, *args).sort_index(
554556
axis=1))
555-
tm.assert_frame_equal(expected, getattr(gb, op)(*args))
557+
tm.assert_frame_equal(
558+
expected,
559+
getattr(gb, op)(*args).sort_index(axis=1))
556560
# individual columns
557561
for c in df:
558562
if c not in ['float', 'int', 'float_missing'

pandas/tests/indexing/test_ix.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,15 @@ def test_ix_loc_setitem_consistency(self):
5353

5454
# GH 8607
5555
# ix setitem consistency
56-
df = DataFrame({'timestamp': [1413840976, 1413842580, 1413760580],
57-
'delta': [1174, 904, 161],
58-
'elapsed': [7673, 9277, 1470]})
59-
expected = DataFrame({'timestamp': pd.to_datetime(
60-
[1413840976, 1413842580, 1413760580], unit='s'),
61-
'delta': [1174, 904, 161],
62-
'elapsed': [7673, 9277, 1470]})
56+
df = DataFrame({'delta': [1174, 904, 161],
57+
'elapsed': [7673, 9277, 1470],
58+
'timestamp': [1413840976, 1413842580, 1413760580]})
59+
expected = DataFrame({'delta': [1174, 904, 161],
60+
'elapsed': [7673, 9277, 1470],
61+
'timestamp': pd.to_datetime(
62+
[1413840976, 1413842580, 1413760580],
63+
unit='s')
64+
})
6365

6466
df2 = df.copy()
6567
df2['timestamp'] = pd.to_datetime(df['timestamp'], unit='s')

pandas/tests/io/formats/test_format.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,8 @@ def test_east_asian_unicode_frame(self):
539539
assert _rep(df) == expected
540540

541541
# column name
542-
df = DataFrame({u'あああああ': [1, 222, 33333, 4],
543-
'b': [u'あ', u'いいい', u'う', u'ええええええ']},
542+
df = DataFrame({'b': [u'あ', u'いいい', u'う', u'ええええええ'],
543+
u'あああああ': [1, 222, 33333, 4]},
544544
index=['a', 'bb', 'c', 'ddd'])
545545
expected = (u" b あああああ\na あ 1\n"
546546
u"bb いいい 222\nc う 33333\n"
@@ -647,8 +647,8 @@ def test_east_asian_unicode_frame(self):
647647
assert _rep(df) == expected
648648

649649
# column name
650-
df = DataFrame({u'あああああ': [1, 222, 33333, 4],
651-
'b': [u'あ', u'いいい', u'う', u'ええええええ']},
650+
df = DataFrame({'b': [u'あ', u'いいい', u'う', u'ええええええ'],
651+
u'あああああ': [1, 222, 33333, 4]},
652652
index=['a', 'bb', 'c', 'ddd'])
653653
expected = (u" b あああああ\n"
654654
u"a あ 1\n"
@@ -733,8 +733,8 @@ def test_east_asian_unicode_frame(self):
733733
assert _rep(df) == expected
734734

735735
# ambiguous unicode
736-
df = DataFrame({u'あああああ': [1, 222, 33333, 4],
737-
'b': [u'あ', u'いいい', u'¡¡', u'ええええええ']},
736+
df = DataFrame({'b': [u'あ', u'いいい', u'¡¡', u'ええええええ'],
737+
u'あああああ': [1, 222, 33333, 4]},
738738
index=['a', 'bb', 'c', '¡¡¡'])
739739
expected = (u" b あああああ\n"
740740
u"a あ 1\n"

pandas/tests/io/formats/test_to_latex.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,18 @@ def test_to_latex_empty(self):
115115
assert result == expected
116116

117117
def test_to_latex_with_formatters(self):
118-
df = DataFrame({'int': [1, 2, 3],
118+
df = DataFrame({'datetime64': [datetime(2016, 1, 1),
119+
datetime(2016, 2, 5),
120+
datetime(2016, 3, 3)],
119121
'float': [1.0, 2.0, 3.0],
122+
'int': [1, 2, 3],
120123
'object': [(1, 2), True, False],
121-
'datetime64': [datetime(2016, 1, 1),
122-
datetime(2016, 2, 5),
123-
datetime(2016, 3, 3)]})
124+
})
124125

125-
formatters = {'int': lambda x: '0x{x:x}'.format(x=x),
126+
formatters = {'datetime64': lambda x: x.strftime('%Y-%m'),
126127
'float': lambda x: '[{x: 4.1f}]'.format(x=x),
128+
'int': lambda x: '0x{x:x}'.format(x=x),
127129
'object': lambda x: '-{x!s}-'.format(x=x),
128-
'datetime64': lambda x: x.strftime('%Y-%m'),
129130
'__index__': lambda x: 'index: {x}'.format(x=x)}
130131
result = df.to_latex(formatters=dict(formatters))
131132

@@ -347,10 +348,10 @@ def test_to_latex_escape(self):
347348
a = 'a'
348349
b = 'b'
349350

350-
test_dict = {u('co^l1'): {a: "a",
351-
b: "b"},
352-
u('co$e^x$'): {a: "a",
353-
b: "b"}}
351+
test_dict = {u('co$e^x$'): {a: "a",
352+
b: "b"},
353+
u('co^l1'): {a: "a",
354+
b: "b"}}
354355

355356
unescaped_result = DataFrame(test_dict).to_latex(escape=False)
356357
escaped_result = DataFrame(test_dict).to_latex(

pandas/tests/io/json/test_pandas.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ def __str__(self):
553553

554554
def test_label_overflow(self):
555555
# GH14256: buffer length not checked when writing label
556-
df = pd.DataFrame({'foo': [1337], 'bar' * 100000: [1]})
556+
df = pd.DataFrame({'bar' * 100000: [1], 'foo': [1337]})
557557
assert df.to_json() == \
558558
'{{"{bar}":{{"0":1}},"foo":{{"0":1337}}}}'.format(
559559
bar=('bar' * 100000))

pandas/tests/reshape/merge/test_merge.py

+22-21
Original file line numberDiff line numberDiff line change
@@ -588,18 +588,18 @@ def test_merge_on_datetime64tz(self):
588588
result = pd.merge(left, right, on='key', how='outer')
589589
assert_frame_equal(result, expected)
590590

591-
left = pd.DataFrame({'value': pd.date_range('20151010', periods=2,
592-
tz='US/Eastern'),
593-
'key': [1, 2]})
594-
right = pd.DataFrame({'value': pd.date_range('20151011', periods=2,
595-
tz='US/Eastern'),
596-
'key': [2, 3]})
591+
left = pd.DataFrame({'key': [1, 2],
592+
'value': pd.date_range('20151010', periods=2,
593+
tz='US/Eastern')})
594+
right = pd.DataFrame({'key': [2, 3],
595+
'value': pd.date_range('20151011', periods=2,
596+
tz='US/Eastern')})
597597
expected = DataFrame({
598+
'key': [1, 2, 3],
598599
'value_x': list(pd.date_range('20151010', periods=2,
599600
tz='US/Eastern')) + [pd.NaT],
600601
'value_y': [pd.NaT] + list(pd.date_range('20151011', periods=2,
601-
tz='US/Eastern')),
602-
'key': [1, 2, 3]})
602+
tz='US/Eastern'))})
603603
result = pd.merge(left, right, on='key', how='outer')
604604
assert_frame_equal(result, expected)
605605
assert result['value_x'].dtype == 'datetime64[ns, US/Eastern]'
@@ -632,31 +632,32 @@ def test_merge_on_periods(self):
632632
result = pd.merge(left, right, on='key', how='outer')
633633
assert_frame_equal(result, expected)
634634

635-
left = pd.DataFrame({'value': pd.period_range('20151010', periods=2,
636-
freq='D'),
637-
'key': [1, 2]})
638-
right = pd.DataFrame({'value': pd.period_range('20151011', periods=2,
639-
freq='D'),
640-
'key': [2, 3]})
635+
left = pd.DataFrame({'key': [1, 2],
636+
'value': pd.period_range('20151010', periods=2,
637+
freq='D')})
638+
right = pd.DataFrame({'key': [2, 3],
639+
'value': pd.period_range('20151011', periods=2,
640+
freq='D')})
641641

642642
exp_x = pd.period_range('20151010', periods=2, freq='D')
643643
exp_y = pd.period_range('20151011', periods=2, freq='D')
644-
expected = DataFrame({'value_x': list(exp_x) + [pd.NaT],
645-
'value_y': [pd.NaT] + list(exp_y),
646-
'key': [1, 2, 3]})
644+
expected = DataFrame({'key': [1, 2, 3],
645+
'value_x': list(exp_x) + [pd.NaT],
646+
'value_y': [pd.NaT] + list(exp_y)})
647647
result = pd.merge(left, right, on='key', how='outer')
648648
assert_frame_equal(result, expected)
649649
assert result['value_x'].dtype == 'object'
650650
assert result['value_y'].dtype == 'object'
651651

652652
def test_indicator(self):
653653
# PR #10054. xref #7412 and closes #8790.
654-
df1 = DataFrame({'col1': [0, 1], 'col_left': [
655-
'a', 'b'], 'col_conflict': [1, 2]})
654+
df1 = DataFrame({'col1': [0, 1], 'col_conflict': [1, 2],
655+
'col_left': ['a', 'b']})
656656
df1_copy = df1.copy()
657657

658-
df2 = DataFrame({'col1': [1, 2, 3, 4, 5], 'col_right': [2, 2, 2, 2, 2],
659-
'col_conflict': [1, 2, 3, 4, 5]})
658+
df2 = DataFrame({'col1': [1, 2, 3, 4, 5],
659+
'col_conflict': [1, 2, 3, 4, 5],
660+
'col_right': [2, 2, 2, 2, 2]})
660661
df2_copy = df2.copy()
661662

662663
df_result = DataFrame({

pandas/tests/reshape/merge/test_merge_ordered.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ def test_empty_sequence_concat(self):
8383
pd.concat([pd.DataFrame(), None])
8484

8585
def test_doc_example(self):
86-
left = DataFrame({'key': ['a', 'c', 'e', 'a', 'c', 'e'],
86+
left = DataFrame({'group': list('aaabbb'),
87+
'key': ['a', 'c', 'e', 'a', 'c', 'e'],
8788
'lvalue': [1, 2, 3] * 2,
88-
'group': list('aaabbb')})
89+
})
8990

9091
right = DataFrame({'key': ['b', 'c', 'd'],
9192
'rvalue': [1, 2, 3]})

pandas/tests/reshape/test_concat.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1542,10 +1542,10 @@ def test_concat_bug_2972(self):
15421542
def test_concat_bug_3602(self):
15431543

15441544
# GH 3602, duplicate columns
1545-
df1 = DataFrame({'firmNo': [0, 0, 0, 0], 'stringvar': [
1546-
'rrr', 'rrr', 'rrr', 'rrr'], 'prc': [6, 6, 6, 6]})
1547-
df2 = DataFrame({'misc': [1, 2, 3, 4], 'prc': [
1548-
6, 6, 6, 6], 'C': [9, 10, 11, 12]})
1545+
df1 = DataFrame({'firmNo': [0, 0, 0, 0], 'prc': [6, 6, 6, 6],
1546+
'stringvar': ['rrr', 'rrr', 'rrr', 'rrr']})
1547+
df2 = DataFrame({'C': [9, 10, 11, 12], 'misc': [1, 2, 3, 4],
1548+
'prc': [6, 6, 6, 6]})
15491549
expected = DataFrame([[0, 6, 'rrr', 9, 1, 6],
15501550
[0, 6, 'rrr', 10, 2, 6],
15511551
[0, 6, 'rrr', 11, 3, 6],

pandas/tests/reshape/test_melt.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -589,11 +589,11 @@ def test_nonnumeric_suffix(self):
589589

590590
def test_mixed_type_suffix(self):
591591
df = pd.DataFrame({
592-
'treatment_1': [1.0, 2.0],
593-
'treatment_foo': [3.0, 4.0],
594-
'result_foo': [5.0, 6.0],
592+
'A': ['X1', 'X2'],
595593
'result_1': [0, 9],
596-
'A': ['X1', 'X2']})
594+
'result_foo': [5.0, 6.0],
595+
'treatment_1': [1.0, 2.0],
596+
'treatment_foo': [3.0, 4.0]})
597597
expected = pd.DataFrame({
598598
'A': ['X1', 'X2', 'X1', 'X2'],
599599
'colname': ['1', '1', 'foo', 'foo'],

pandas/tests/reshape/test_reshape.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ def test_basic_types(self, sparse, dtype):
100100
expected_counts = {'int64': 1, 'object': 1}
101101
expected_counts[dtype_name] = 3 + expected_counts.get(dtype_name, 0)
102102

103-
expected = Series(expected_counts).sort_values()
104-
tm.assert_series_equal(result.get_dtype_counts().sort_values(),
103+
expected = Series(expected_counts).sort_index()
104+
tm.assert_series_equal(result.get_dtype_counts().sort_index(),
105105
expected)
106106

107107
def test_just_na(self, sparse):
@@ -212,10 +212,10 @@ def test_dataframe_dummies_prefix_str(self, df, sparse):
212212
def test_dataframe_dummies_subset(self, df, sparse):
213213
result = get_dummies(df, prefix=['from_A'], columns=['A'],
214214
sparse=sparse)
215-
expected = DataFrame({'from_A_a': [1, 0, 1],
216-
'from_A_b': [0, 1, 0],
217-
'B': ['b', 'b', 'c'],
218-
'C': [1, 2, 3]}, dtype=np.uint8)
215+
expected = DataFrame({'B': ['b', 'b', 'c'],
216+
'C': [1, 2, 3],
217+
'from_A_a': [1, 0, 1],
218+
'from_A_b': [0, 1, 0]}, dtype=np.uint8)
219219
expected[['C']] = df[['C']]
220220
assert_frame_equal(result, expected)
221221

@@ -249,16 +249,16 @@ def test_dataframe_dummies_prefix_sep_bad_length(self, df, sparse):
249249

250250
def test_dataframe_dummies_prefix_dict(self, sparse):
251251
prefixes = {'A': 'from_A', 'B': 'from_B'}
252-
df = DataFrame({'A': ['a', 'b', 'a'],
253-
'B': ['b', 'b', 'c'],
254-
'C': [1, 2, 3]})
252+
df = DataFrame({'C': [1, 2, 3],
253+
'A': ['a', 'b', 'a'],
254+
'B': ['b', 'b', 'c']})
255255
result = get_dummies(df, prefix=prefixes, sparse=sparse)
256256

257-
expected = DataFrame({'from_A_a': [1, 0, 1],
257+
expected = DataFrame({'C': [1, 2, 3],
258+
'from_A_a': [1, 0, 1],
258259
'from_A_b': [0, 1, 0],
259260
'from_B_b': [1, 1, 0],
260-
'from_B_c': [0, 0, 1],
261-
'C': [1, 2, 3]})
261+
'from_B_c': [0, 0, 1]})
262262

263263
columns = ['from_A_a', 'from_A_b', 'from_B_b', 'from_B_c']
264264
expected[columns] = expected[columns].astype(np.uint8)

0 commit comments

Comments
 (0)