Skip to content

Commit 2bd0afe

Browse files
committed
recommend from_dict(dict()) and from_dict(OrderedDict())
1 parent ca940ff commit 2bd0afe

File tree

7 files changed

+57
-56
lines changed

7 files changed

+57
-56
lines changed

doc/source/whatsnew/v0.23.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ Deprecations
329329
- ``Series.valid`` is deprecated. Use :meth:`Series.dropna` instead (:issue:`18800`).
330330
- :func:`read_excel` has deprecated the ``skip_footer`` parameter. Use ``skipfooter`` instead (:issue:`18836`)
331331
- The ``is_copy`` attribute is deprecated and will be removed in a future version (:issue:`18801`).
332-
- :func:``DataFrame.from_items`` is deprecated. Use ``DataFrame.from_dict(OrderedDict())`` instead (:issue:`17320`)
332+
- :func:``DataFrame.from_items`` is deprecated. Use :func:``DataFrame.from_dict()`` instead, or :func:``DataFrame.from_dict(OrderedDict())`` if you wish to preserve the key order (:issue:`17320`)
333333

334334
.. _whatsnew_0230.prior_deprecations:
335335

pandas/core/frame.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,9 @@ def to_records(self, index=True, convert_datetime64=True):
12431243
def from_items(cls, items, columns=None, orient='columns'):
12441244
"""
12451245
DEPRECATED: from_items is deprecated and will be removed in a
1246-
future version. Use :meth:`DataFrame(dict())` instead.
1246+
future version. Use :meth:`DataFrame.from_dict(dict())`
1247+
instead. :meth:`DataFrame.from_dict(OrderedDict(...))` may be used
1248+
to preserve the key order.
12471249
12481250
Convert (key, value) pairs to DataFrame. The keys will be the axis
12491251
index (usually the columns, but depends on the specified
@@ -1267,7 +1269,9 @@ def from_items(cls, items, columns=None, orient='columns'):
12671269
"""
12681270

12691271
warnings.warn("from_items is deprecated. Please use "
1270-
"DataFrame(dict()) instead.",
1272+
"DataFrame.from_dict(dict()) instead. "
1273+
"DataFrame.from_dict(OrderedDict()) may be used to "
1274+
"preserve the key order.",
12711275
FutureWarning, stacklevel=2)
12721276

12731277
keys, values = lzip(*items)

pandas/io/stata.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -1572,8 +1572,7 @@ def read(self, nrows=None, convert_dates=None,
15721572
else:
15731573
data_formatted.append((col, data[col]))
15741574
if requires_type_conversion:
1575-
data = DataFrame(OrderedDict(data_formatted),
1576-
columns=OrderedDict(data_formatted).keys())
1575+
data = DataFrame.from_dict(OrderedDict(data_formatted))
15771576
del data_formatted
15781577

15791578
self._do_convert_missing(data, convert_missing)
@@ -1611,8 +1610,7 @@ def read(self, nrows=None, convert_dates=None,
16111610
convert = True
16121611
retyped_data.append((col, data[col].astype(dtype)))
16131612
if convert:
1614-
data = DataFrame(OrderedDict(retyped_data),
1615-
columns=OrderedDict(retyped_data).keys())
1613+
data = DataFrame.from_dict(OrderedDict(retyped_data))
16161614

16171615
if index_col is not None:
16181616
data = data.set_index(data.pop(index_col))
@@ -1725,8 +1723,7 @@ def _do_convert_categoricals(self, data, value_label_dict, lbllist,
17251723
cat_converted_data.append((col, cat_data))
17261724
else:
17271725
cat_converted_data.append((col, data[col]))
1728-
data = DataFrame(OrderedDict(cat_converted_data),
1729-
columns=OrderedDict(cat_converted_data).keys())
1726+
data = DataFrame.from_dict(OrderedDict(cat_converted_data))
17301727
return data
17311728

17321729
def data_label(self):
@@ -2001,8 +1998,7 @@ def _prepare_categoricals(self, data):
20011998
data_formatted.append((col, values))
20021999
else:
20032000
data_formatted.append((col, data[col]))
2004-
return DataFrame(OrderedDict(data_formatted),
2005-
columns=OrderedDict(data_formatted).keys())
2001+
return DataFrame.from_dict(OrderedDict(data_formatted))
20062002

20072003
def _replace_nans(self, data):
20082004
# return data

pandas/tests/frame/test_nonunique_indexes.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,8 @@ def check(result, expected=None):
328328

329329
df1r = df1.reindex_like(df2)
330330
result = df1r == df2
331-
expected = DataFrame([[False, True], [True, False], [False, False],
332-
[True, False]], columns=['A', 'A'])
331+
expected = DataFrame([[False, True], [True, False], [False, False], [
332+
True, False]], columns=['A', 'A'])
333333
assert_frame_equal(result, expected)
334334

335335
# mixed column selection

pandas/tests/io/parser/common.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import re
99
import sys
1010
from datetime import datetime
11+
from collections import OrderedDict
1112

1213
import pytest
1314
import numpy as np
@@ -924,8 +925,9 @@ def test_float_parser(self):
924925

925926
def test_scientific_no_exponent(self):
926927
# see gh-12215
927-
df = DataFrame({'w': ['2e'], 'x': ['3E'],
928-
'y': ['42e'], 'z': ['632E']})
928+
df = DataFrame.from_dict(OrderedDict([('w', ['2e']), ('x', ['3E']),
929+
('y', ['42e']),
930+
('z', ['632E'])]))
929931
data = df.to_csv(index=False)
930932
for prec in self.float_precision_choices:
931933
df_roundtrip = self.read_csv(

pandas/tests/io/test_excel.py

+39-40
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from distutils.version import LooseVersion
77
from functools import partial
88
from warnings import catch_warnings
9+
from collections import OrderedDict
910

1011
import numpy as np
1112
import pytest
@@ -315,19 +316,17 @@ def test_excel_table(self):
315316

316317
def test_reader_special_dtypes(self):
317318

318-
expected = DataFrame({
319-
"IntCol": [1, 2, -3, 4, 0],
320-
"FloatCol": [1.25, 2.25, 1.83, 1.92, 0.0000000005],
321-
"BoolCol": [True, False, True, True, False],
322-
"StrCol": [1, 2, 3, 4, 5],
319+
expected = DataFrame.from_dict(OrderedDict([
320+
("IntCol", [1, 2, -3, 4, 0]),
321+
("FloatCol", [1.25, 2.25, 1.83, 1.92, 0.0000000005]),
322+
("BoolCol", [True, False, True, True, False]),
323+
("StrCol", [1, 2, 3, 4, 5]),
323324
# GH5394 - this is why convert_float isn't vectorized
324-
"Str2Col": ["a", 3, "c", "d", "e"],
325-
"DateCol": [datetime(2013, 10, 30), datetime(2013, 10, 31),
326-
datetime(1905, 1, 1), datetime(2013, 12, 14),
327-
datetime(2015, 3, 14)]
328-
}, columns=["IntCol", "FloatCol", "BoolCol",
329-
"StrCol", "Str2Col", "DateCol"])
330-
325+
("Str2Col", ["a", 3, "c", "d", "e"]),
326+
("DateCol", [datetime(2013, 10, 30), datetime(2013, 10, 31),
327+
datetime(1905, 1, 1), datetime(2013, 12, 14),
328+
datetime(2015, 3, 14)])
329+
]))
331330
basename = 'test_types'
332331

333332
# should read in correctly and infer types
@@ -364,12 +363,12 @@ def test_reader_converters(self):
364363

365364
basename = 'test_converters'
366365

367-
expected = DataFrame({
368-
"IntCol": [1, 2, -3, -1000, 0],
369-
"FloatCol": [12.5, np.nan, 18.3, 19.2, 0.000000005],
370-
"BoolCol": ['Found', 'Found', 'Found', 'Not found', 'Found'],
371-
"StrCol": ['1', np.nan, '3', '4', '5'],
372-
}, columns=['IntCol', 'FloatCol', 'BoolCol', 'StrCol'])
366+
expected = DataFrame.from_dict(OrderedDict([
367+
("IntCol", [1, 2, -3, -1000, 0]),
368+
("FloatCol", [12.5, np.nan, 18.3, 19.2, 0.000000005]),
369+
("BoolCol", ['Found', 'Found', 'Found', 'Not found', 'Found']),
370+
("StrCol", ['1', np.nan, '3', '4', '5']),
371+
]))
373372

374373
converters = {'IntCol': lambda x: int(x) if x != '' else -1000,
375374
'FloatCol': lambda x: 10 * x if x else np.nan,
@@ -719,30 +718,30 @@ def test_reader_seconds(self):
719718

720719
if LooseVersion(xlrd.__VERSION__) >= LooseVersion("0.9.3"):
721720
# Xlrd >= 0.9.3 can handle Excel milliseconds.
722-
expected = DataFrame({"Time": [time(1, 2, 3),
723-
time(2, 45, 56, 100000),
724-
time(4, 29, 49, 200000),
725-
time(6, 13, 42, 300000),
726-
time(7, 57, 35, 400000),
727-
time(9, 41, 28, 500000),
728-
time(11, 25, 21, 600000),
729-
time(13, 9, 14, 700000),
730-
time(14, 53, 7, 800000),
731-
time(16, 37, 0, 900000),
732-
time(18, 20, 54)]})
721+
expected = DataFrame.from_dict({"Time": [time(1, 2, 3),
722+
time(2, 45, 56, 100000),
723+
time(4, 29, 49, 200000),
724+
time(6, 13, 42, 300000),
725+
time(7, 57, 35, 400000),
726+
time(9, 41, 28, 500000),
727+
time(11, 25, 21, 600000),
728+
time(13, 9, 14, 700000),
729+
time(14, 53, 7, 800000),
730+
time(16, 37, 0, 900000),
731+
time(18, 20, 54)]})
733732
else:
734733
# Xlrd < 0.9.3 rounds Excel milliseconds.
735-
expected = DataFrame({"Time": [time(1, 2, 3),
736-
time(2, 45, 56),
737-
time(4, 29, 49),
738-
time(6, 13, 42),
739-
time(7, 57, 35),
740-
time(9, 41, 29),
741-
time(11, 25, 22),
742-
time(13, 9, 15),
743-
time(14, 53, 8),
744-
time(16, 37, 1),
745-
time(18, 20, 54)]})
734+
expected = DataFrame.from_dict({"Time": [time(1, 2, 3),
735+
time(2, 45, 56),
736+
time(4, 29, 49),
737+
time(6, 13, 42),
738+
time(7, 57, 35),
739+
time(9, 41, 29),
740+
time(11, 25, 22),
741+
time(13, 9, 15),
742+
time(14, 53, 8),
743+
time(16, 37, 1),
744+
time(18, 20, 54)]})
746745

747746
actual = self.get_exceldf('times_1900', 'Sheet1')
748747
tm.assert_frame_equal(actual, expected)

pandas/tests/io/test_stata.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ def test_categorical_order(self, file):
946946
cols.append((col, pd.Categorical.from_codes(codes, labels)))
947947
else:
948948
cols.append((col, pd.Series(labels, dtype=np.float32)))
949-
expected = DataFrame(dict(cols), columns=OrderedDict(cols).keys())
949+
expected = DataFrame.from_dict(OrderedDict(cols))
950950

951951
# Read with and with out categoricals, ensure order is identical
952952
file = getattr(self, file)

0 commit comments

Comments
 (0)