Skip to content

Commit 3b35993

Browse files
committed
More performant, more warnings
1 parent 32128eb commit 3b35993

File tree

14 files changed

+86
-34
lines changed

14 files changed

+86
-34
lines changed

pandas/core/frame.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11114,6 +11114,7 @@ def _append_internal(
1111411114
result = concat(
1111511115
[self, row_df],
1111611116
ignore_index=ignore_index,
11117+
sort=False,
1111711118
)
1111811119
return result.__finalize__(self, method="append")
1111911120

pandas/core/generic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6521,7 +6521,7 @@ def astype(
65216521
return self.copy(deep=False)
65226522

65236523
# GH 19920: retain column metadata after concat
6524-
result = concat(results, axis=1)
6524+
result = concat(results, axis=1, sort=False)
65256525
# GH#40810 retain subclass
65266526
# error: Incompatible types in assignment
65276527
# (expression has type "Self", variable has type "DataFrame")

pandas/core/groupby/groupby.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5236,7 +5236,7 @@ def shift(
52365236
return (
52375237
shifted_dataframes[0]
52385238
if len(shifted_dataframes) == 1
5239-
else concat(shifted_dataframes, axis=1)
5239+
else concat(shifted_dataframes, axis=1, sort=False)
52405240
)
52415241

52425242
@final

pandas/core/reshape/concat.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,15 @@ def concat(
418418
non_concat_axis = [obj.index for obj in objs]
419419

420420
if all(isinstance(index, DatetimeIndex) for index in non_concat_axis):
421-
from pandas.core.indexes.api import union_indexes
422-
423-
no_sort_result_index = union_indexes(non_concat_axis, sort=False)
424-
if not no_sort_result_index.is_monotonic_increasing:
421+
warn = any(
422+
id(prev) != id(curr)
423+
for prev, curr in zip(non_concat_axis, non_concat_axis[1:])
424+
) and any(
425+
prev[-1] > curr[0]
426+
for prev, curr in zip(non_concat_axis, non_concat_axis[1:])
427+
if not prev.empty and not curr.empty
428+
)
429+
if warn:
425430
msg = (
426431
"Sorting by default when concatenating all DatetimeIndex is "
427432
"deprecated. In the future, pandas will respect the default "

pandas/core/reshape/pivot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ def pivot_table(
263263
pieces.append(_table)
264264
keys.append(getattr(func, "__name__", func))
265265

266-
table = concat(pieces, keys=keys, axis=1)
266+
table = concat(pieces, keys=keys, axis=1, sort=False)
267267
return table.__finalize__(data, method="pivot_table")
268268

269269
table = __internal_pivot_table(

pandas/io/pytables.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,9 @@ def func(_start, _stop, _where):
11091109
]
11101110

11111111
# concat and return
1112-
return concat(objs, axis=axis, verify_integrity=False)._consolidate()
1112+
return concat(
1113+
objs, axis=axis, verify_integrity=False, sort=False
1114+
)._consolidate()
11131115

11141116
# create the iterator
11151117
it = TableIterator(
@@ -4860,7 +4862,7 @@ def read(
48604862
if len(frames) == 1:
48614863
df = frames[0]
48624864
else:
4863-
df = concat(frames, axis=1)
4865+
df = concat(frames, axis=1, sort=False)
48644866

48654867
selection = Selection(self, where=where, start=start, stop=stop)
48664868
# apply the selection filters & axis orderings

pandas/tests/io/pytables/test_append.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from pandas._libs.tslibs import Timestamp
99
from pandas.compat import PY312
10+
from pandas.errors import Pandas4Warning
1011

1112
import pandas as pd
1213
from pandas import (
@@ -887,7 +888,9 @@ def test_append_to_multiple(setup_path):
887888
)
888889
df2 = df1.copy().rename(columns="{}_2".format)
889890
df2["foo"] = "bar"
890-
df = concat([df1, df2], axis=1)
891+
msg = "Sorting by default when concatenating all DatetimeIndex is deprecated"
892+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
893+
df = concat([df1, df2], axis=1)
891894

892895
with ensure_clean_store(setup_path) as store:
893896
# exceptions
@@ -928,7 +931,9 @@ def test_append_to_multiple_dropna(setup_path):
928931
index=date_range("2000-01-01", periods=10, freq="B"),
929932
).rename(columns="{}_2".format)
930933
df1.iloc[1, df1.columns.get_indexer(["A", "B"])] = np.nan
931-
df = concat([df1, df2], axis=1)
934+
msg = "Sorting by default when concatenating all DatetimeIndex is deprecated"
935+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
936+
df = concat([df1, df2], axis=1)
932937

933938
with ensure_clean_store(setup_path) as store:
934939
# dropna=True should guarantee rows are synchronized
@@ -949,7 +954,9 @@ def test_append_to_multiple_dropna_false(setup_path):
949954
)
950955
df2 = df1.copy().rename(columns="{}_2".format)
951956
df1.iloc[1, df1.columns.get_indexer(["A", "B"])] = np.nan
952-
df = concat([df1, df2], axis=1)
957+
msg = "Sorting by default when concatenating all DatetimeIndex is deprecated"
958+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
959+
df = concat([df1, df2], axis=1)
953960

954961
with (
955962
ensure_clean_store(setup_path) as store,

pandas/tests/io/pytables/test_select.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from pandas._libs.tslibs import Timestamp
55
from pandas.compat import PY312
6+
from pandas.errors import Pandas4Warning
67

78
import pandas as pd
89
from pandas import (
@@ -411,7 +412,9 @@ def test_select_iterator(tmp_path, setup_path):
411412
df2["foo"] = "bar"
412413
store.append("df2", df2)
413414

414-
df = concat([df1, df2], axis=1)
415+
msg = "Sorting by default when concatenating all DatetimeIndex is deprecated"
416+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
417+
df = concat([df1, df2], axis=1)
415418

416419
# full selection
417420
expected = store.select_as_multiple(["df1", "df2"], selector="df1")
@@ -901,7 +904,9 @@ def test_select_as_multiple(setup_path):
901904
result = store.select_as_multiple(
902905
["df1", "df2"], where=["A>0", "B>0"], selector="df1"
903906
)
904-
expected = concat([df1, df2], axis=1)
907+
msg = "Sorting by default when concatenating all DatetimeIndex is deprecated"
908+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
909+
expected = concat([df1, df2], axis=1)
905910
expected = expected[(expected.A > 0) & (expected.B > 0)]
906911
tm.assert_frame_equal(result, expected, check_freq=False)
907912
# FIXME: 2021-01-20 this is failing with freq None vs 4B on some builds
@@ -910,7 +915,9 @@ def test_select_as_multiple(setup_path):
910915
result = store.select_as_multiple(
911916
["df1", "df2"], where="index>df2.index[4]", selector="df2"
912917
)
913-
expected = concat([df1, df2], axis=1)
918+
msg = "Sorting by default when concatenating all DatetimeIndex is deprecated"
919+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
920+
expected = concat([df1, df2], axis=1)
914921
expected = expected[5:]
915922
tm.assert_frame_equal(result, expected)
916923

pandas/tests/io/pytables/test_store.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import pytest
99

1010
from pandas.compat import PY312
11+
from pandas.errors import Pandas4Warning
1112

1213
import pandas as pd
1314
from pandas import (
@@ -732,9 +733,13 @@ def test_coordinates(setup_path):
732733
c = store.select_as_coordinates("df1", ["A>0", "B>0"])
733734
df1_result = store.select("df1", c)
734735
df2_result = store.select("df2", c)
735-
result = concat([df1_result, df2_result], axis=1)
736+
msg = "Sorting by default when concatenating all DatetimeIndex is deprecated"
737+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
738+
result = concat([df1_result, df2_result], axis=1)
736739

737-
expected = concat([df1, df2], axis=1)
740+
msg = "Sorting by default when concatenating all DatetimeIndex is deprecated"
741+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
742+
expected = concat([df1, df2], axis=1)
738743
expected = expected[(expected.A > 0) & (expected.B > 0)]
739744
tm.assert_frame_equal(result, expected, check_freq=False)
740745
# FIXME: 2021-01-18 on some (mostly windows) builds we get freq=None

pandas/tests/resample/test_datetime_index.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -602,11 +602,13 @@ def test_resample_ohlc_dataframe(unit):
602602
df.index = df.index.as_unit(unit)
603603
df.columns.name = "Cols"
604604
res = df.resample("h").ohlc()
605-
exp = pd.concat(
606-
[df["VOLUME"].resample("h").ohlc(), df["PRICE"].resample("h").ohlc()],
607-
axis=1,
608-
keys=df.columns,
609-
)
605+
msg = "Sorting by default when concatenating all DatetimeIndex is deprecated"
606+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
607+
exp = pd.concat(
608+
[df["VOLUME"].resample("h").ohlc(), df["PRICE"].resample("h").ohlc()],
609+
axis=1,
610+
keys=df.columns,
611+
)
610612
assert exp.columns.names[0] == "Cols"
611613
tm.assert_frame_equal(exp, res)
612614

0 commit comments

Comments
 (0)