Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
labeling information
"""
import collections
from collections import OrderedDict, abc
from collections import abc
from io import StringIO
import itertools
import sys
Expand Down Expand Up @@ -8189,10 +8189,10 @@ def isin(self, values):

def _from_nested_dict(data):
# TODO: this should be seriously cythonized
new_data = OrderedDict()
new_data = {}
for index, s in data.items():
for col, v in s.items():
new_data[col] = new_data.get(col, OrderedDict())
new_data[col] = new_data.get(col, {})
new_data[col][index] = v
return new_data

Expand Down
5 changes: 2 additions & 3 deletions pandas/io/excel/_base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import abc
from collections import OrderedDict
from datetime import date, datetime, timedelta
from io import BytesIO
import os
Expand Down Expand Up @@ -429,9 +428,9 @@ def parse(
sheets = [sheet_name]

# handle same-type duplicates.
sheets = list(OrderedDict.fromkeys(sheets).keys())
sheets = list(dict.fromkeys(sheets).keys())

output = OrderedDict()
output = {}

for asheetname in sheets:
if verbose:
Expand Down
17 changes: 8 additions & 9 deletions pandas/io/stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
http://www.statsmodels.org/devel/
"""

from collections import OrderedDict
import datetime
from io import BytesIO
import os
Expand Down Expand Up @@ -1677,7 +1676,7 @@ def read(
else:
data_formatted.append((col, data[col]))
if requires_type_conversion:
data = DataFrame.from_dict(OrderedDict(data_formatted))
data = DataFrame.from_dict(dict(data_formatted))
del data_formatted

data = self._do_convert_missing(data, convert_missing)
Expand Down Expand Up @@ -1716,7 +1715,7 @@ def any_startswith(x: str) -> bool:
convert = True
retyped_data.append((col, data[col].astype(dtype)))
if convert:
data = DataFrame.from_dict(OrderedDict(retyped_data))
data = DataFrame.from_dict(dict(retyped_data))

if index_col is not None:
data = data.set_index(data.pop(index_col))
Expand Down Expand Up @@ -1846,7 +1845,7 @@ def _do_convert_categoricals(
cat_converted_data.append((col, cat_data))
else:
cat_converted_data.append((col, data[col]))
data = DataFrame.from_dict(OrderedDict(cat_converted_data))
data = DataFrame.from_dict(dict(cat_converted_data))
return data

@property
Expand Down Expand Up @@ -2195,7 +2194,7 @@ def _prepare_categoricals(self, data):
data_formatted.append((col, values))
else:
data_formatted.append((col, data[col]))
return DataFrame.from_dict(OrderedDict(data_formatted))
return DataFrame.from_dict(dict(data_formatted))

def _replace_nans(self, data):
# return data
Expand Down Expand Up @@ -2674,7 +2673,7 @@ def __init__(self, df, columns, version=117, byteorder=None):

self.df = df
self.columns = columns
self._gso_table = OrderedDict((("", (0, 0)),))
self._gso_table = dict((("", (0, 0)),))
if byteorder is None:
byteorder = sys.byteorder
self._byteorder = _set_endianness(byteorder)
Expand Down Expand Up @@ -2704,7 +2703,7 @@ def generate_table(self):

Returns
-------
gso_table : OrderedDict
gso_table : dict
Ordered dictionary using the string found as keys
and their lookup position (v,o) as values
gso_df : DataFrame
Expand Down Expand Up @@ -2762,7 +2761,7 @@ def generate_blob(self, gso_table):

Parameters
----------
gso_table : OrderedDict
gso_table : dict
Ordered dictionary (str, vo)

Returns
Expand Down Expand Up @@ -2992,7 +2991,7 @@ def _write_map(self):
the map with 0s. The second call writes the final map locations when
all blocks have been written."""
if self._map is None:
self._map = OrderedDict(
self._map = dict(
(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A last one missing here, else looks good.

("stata_data", 0),
("map", self._file.tell()),
Expand Down
39 changes: 13 additions & 26 deletions pandas/tests/groupby/aggregate/test_aggregate.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
test .agg behavior / note that .apply is tested generally in test_groupby.py
"""
from collections import OrderedDict
import functools

import numpy as np
Expand Down Expand Up @@ -176,10 +175,10 @@ def test_aggregate_str_func(tsframe, groupbyfunc):

# group frame by function dict
result = grouped.agg(
OrderedDict([["A", "var"], ["B", "std"], ["C", "mean"], ["D", "sem"]])
dict([["A", "var"], ["B", "std"], ["C", "mean"], ["D", "sem"]])
)
expected = DataFrame(
OrderedDict(
dict(
[
["A", grouped["A"].var()],
["B", grouped["B"].std()],
Expand Down Expand Up @@ -261,22 +260,20 @@ def test_multiple_functions_tuples_and_non_tuples(df):
def test_more_flexible_frame_multi_function(df):
grouped = df.groupby("A")

exmean = grouped.agg(OrderedDict([["C", np.mean], ["D", np.mean]]))
exstd = grouped.agg(OrderedDict([["C", np.std], ["D", np.std]]))
exmean = grouped.agg(dict([["C", np.mean], ["D", np.mean]]))
exstd = grouped.agg(dict([["C", np.std], ["D", np.std]]))

expected = concat([exmean, exstd], keys=["mean", "std"], axis=1)
expected = expected.swaplevel(0, 1, axis=1).sort_index(level=0, axis=1)

d = OrderedDict([["C", [np.mean, np.std]], ["D", [np.mean, np.std]]])
d = dict([["C", [np.mean, np.std]], ["D", [np.mean, np.std]]])
result = grouped.aggregate(d)

tm.assert_frame_equal(result, expected)

# be careful
result = grouped.aggregate(OrderedDict([["C", np.mean], ["D", [np.mean, np.std]]]))
expected = grouped.aggregate(
OrderedDict([["C", np.mean], ["D", [np.mean, np.std]]])
)
result = grouped.aggregate(dict([["C", np.mean], ["D", [np.mean, np.std]]]))
expected = grouped.aggregate(dict([["C", np.mean], ["D", [np.mean, np.std]]]))
tm.assert_frame_equal(result, expected)

def foo(x):
Expand All @@ -288,13 +285,11 @@ def bar(x):
# this uses column selection & renaming
msg = r"nested renamer is not supported"
with pytest.raises(SpecificationError, match=msg):
d = OrderedDict(
[["C", np.mean], ["D", OrderedDict([["foo", np.mean], ["bar", np.std]])]]
)
d = dict([["C", np.mean], ["D", dict([["foo", np.mean], ["bar", np.std]])]])
grouped.aggregate(d)

# But without renaming, these functions are OK
d = OrderedDict([["C", [np.mean]], ["D", [foo, bar]]])
d = dict([["C", [np.mean]], ["D", [foo, bar]]])
grouped.aggregate(d)


Expand All @@ -303,26 +298,20 @@ def test_multi_function_flexible_mix(df):
grouped = df.groupby("A")

# Expected
d = OrderedDict(
[["C", OrderedDict([["foo", "mean"], ["bar", "std"]])], ["D", {"sum": "sum"}]]
)
d = dict([["C", dict([["foo", "mean"], ["bar", "std"]])], ["D", {"sum": "sum"}]])
# this uses column selection & renaming
msg = r"nested renamer is not supported"
with pytest.raises(SpecificationError, match=msg):
grouped.aggregate(d)

# Test 1
d = OrderedDict(
[["C", OrderedDict([["foo", "mean"], ["bar", "std"]])], ["D", "sum"]]
)
d = dict([["C", dict([["foo", "mean"], ["bar", "std"]])], ["D", "sum"]])
# this uses column selection & renaming
with pytest.raises(SpecificationError, match=msg):
grouped.aggregate(d)

# Test 2
d = OrderedDict(
[["C", OrderedDict([["foo", "mean"], ["bar", "std"]])], ["D", ["sum"]]]
)
d = dict([["C", dict([["foo", "mean"], ["bar", "std"]])], ["D", ["sum"]]])
# this uses column selection & renaming
with pytest.raises(SpecificationError, match=msg):
grouped.aggregate(d)
Expand Down Expand Up @@ -642,9 +631,7 @@ def test_maybe_mangle_lambdas_args(self):
assert func["A"][0](0, 2, b=3) == (0, 2, 3)

def test_maybe_mangle_lambdas_named(self):
func = OrderedDict(
[("C", np.mean), ("D", OrderedDict([("foo", np.mean), ("bar", np.mean)]))]
)
func = dict([("C", np.mean), ("D", dict([("foo", np.mean), ("bar", np.mean)]))])
result = _maybe_mangle_lambdas(func)
assert result == func

Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/groupby/aggregate/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
test all other .agg behavior
"""

from collections import OrderedDict
import datetime as dt
from functools import partial

Expand Down Expand Up @@ -97,7 +96,7 @@ def test_agg_period_index():
s1 = Series(np.random.rand(len(index)), index=index)
s2 = Series(np.random.rand(len(index)), index=index)
series = [("s1", s1), ("s2", s2)]
df = DataFrame.from_dict(OrderedDict(series))
df = DataFrame.from_dict(dict(series))
grouped = df.groupby(df.index.month)
list(grouped)

Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/groupby/test_categorical.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from collections import OrderedDict
from datetime import datetime

import numpy as np
Expand Down Expand Up @@ -1204,7 +1203,7 @@ def test_seriesgroupby_observed_apply_dict(df_cat, observed, index, data):
# GH 24880
expected = Series(data=data, index=index, name="C")
result = df_cat.groupby(["A", "B"], observed=observed)["C"].apply(
lambda x: OrderedDict([("min", x.min()), ("max", x.max())])
lambda x: dict([("min", x.min()), ("max", x.max())])
)
tm.assert_series_equal(result, expected)

Expand Down
5 changes: 2 additions & 3 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from collections import OrderedDict
from datetime import datetime
from decimal import Decimal
from io import StringIO
Expand Down Expand Up @@ -598,7 +597,7 @@ def test_groupby_as_index_agg(df):
expected = grouped.mean()
tm.assert_frame_equal(result, expected)

result2 = grouped.agg(OrderedDict([["C", np.mean], ["D", np.sum]]))
result2 = grouped.agg(dict([["C", np.mean], ["D", np.sum]]))
expected2 = grouped.mean()
expected2["D"] = grouped.sum()["D"]
tm.assert_frame_equal(result2, expected2)
Expand All @@ -617,7 +616,7 @@ def test_groupby_as_index_agg(df):
expected = grouped.mean()
tm.assert_frame_equal(result, expected)

result2 = grouped.agg(OrderedDict([["C", np.mean], ["D", np.sum]]))
result2 = grouped.agg(dict([["C", np.mean], ["D", np.sum]]))
expected2 = grouped.mean()
expected2["D"] = grouped.sum()["D"]
tm.assert_frame_equal(result2, expected2)
Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/indexes/multi/test_constructor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from collections import OrderedDict

import numpy as np
import pytest

Expand Down Expand Up @@ -654,7 +652,7 @@ def test_from_frame_error(non_frame):
def test_from_frame_dtype_fidelity():
# GH 22420
df = pd.DataFrame(
OrderedDict(
dict(
[
("dates", pd.date_range("19910905", periods=6, tz="US/Eastern")),
("a", [1, 1, 1, 2, 2, 2]),
Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/indexes/multi/test_conversion.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from collections import OrderedDict

import numpy as np
import pytest

Expand Down Expand Up @@ -107,7 +105,7 @@ def test_to_frame_dtype_fidelity():
original_dtypes = {name: mi.levels[i].dtype for i, name in enumerate(mi.names)}

expected_df = pd.DataFrame(
OrderedDict(
dict(
[
("dates", pd.date_range("19910905", periods=6, tz="US/Eastern")),
("a", [1, 1, 1, 2, 2, 2]),
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/indexes/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
The tests in this package are to ensure the proper resultant dtypes of
set operations.
"""
from collections import OrderedDict
import itertools as it

import numpy as np
Expand All @@ -16,7 +15,7 @@
from pandas.tests.indexes.conftest import indices_dict
import pandas.util.testing as tm

COMPATIBLE_INCONSISTENT_PAIRS = OrderedDict(
COMPATIBLE_INCONSISTENT_PAIRS = dict(
[
((Int64Index, RangeIndex), (tm.makeIntIndex, tm.makeRangeIndex)),
((Float64Index, Int64Index), (tm.makeFloatIndex, tm.makeIntIndex)),
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/io/test_stata.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from collections import OrderedDict
import datetime as dt
from datetime import datetime
import gzip
Expand Down Expand Up @@ -1029,7 +1028,7 @@ def test_categorical_order(self, file):
cols.append((col, pd.Categorical.from_codes(codes, labels)))
else:
cols.append((col, pd.Series(labels, dtype=np.float32)))
expected = DataFrame.from_dict(OrderedDict(cols))
expected = DataFrame.from_dict(dict(cols))

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