-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
TST: collect .insert tests #32909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
TST: collect .insert tests #32909
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
""" | ||
test_insert is specifically for the DataFrame.insert method; not to be | ||
confused with tests with "insert" in their names that are really testing | ||
__setitem__. | ||
""" | ||
import numpy as np | ||
import pytest | ||
|
||
from pandas import DataFrame, Index | ||
import pandas._testing as tm | ||
|
||
|
||
class TestDataFrameInsert: | ||
def test_insert(self): | ||
df = DataFrame( | ||
np.random.randn(5, 3), index=np.arange(5), columns=["c", "b", "a"] | ||
) | ||
|
||
df.insert(0, "foo", df["a"]) | ||
tm.assert_index_equal(df.columns, Index(["foo", "c", "b", "a"])) | ||
tm.assert_series_equal(df["a"], df["foo"], check_names=False) | ||
|
||
df.insert(2, "bar", df["c"]) | ||
tm.assert_index_equal(df.columns, Index(["foo", "c", "bar", "b", "a"])) | ||
tm.assert_almost_equal(df["c"], df["bar"], check_names=False) | ||
|
||
with pytest.raises(ValueError, match="already exists"): | ||
df.insert(1, "a", df["b"]) | ||
|
||
msg = "cannot insert c, already exists" | ||
with pytest.raises(ValueError, match=msg): | ||
df.insert(1, "c", df["b"]) | ||
|
||
df.columns.name = "some_name" | ||
# preserve columns name field | ||
df.insert(0, "baz", df["c"]) | ||
assert df.columns.name == "some_name" | ||
|
||
def test_insert_column_bug_4032(self): | ||
|
||
# GH#4032, inserting a column and renaming causing errors | ||
df = DataFrame({"b": [1.1, 2.2]}) | ||
|
||
df = df.rename(columns={}) | ||
df.insert(0, "a", [1, 2]) | ||
result = df.rename(columns={}) | ||
|
||
str(result) | ||
expected = DataFrame([[1, 1.1], [2, 2.2]], columns=["a", "b"]) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
df.insert(0, "c", [1.3, 2.3]) | ||
result = df.rename(columns={}) | ||
|
||
str(result) | ||
expected = DataFrame([[1.3, 1, 1.1], [2.3, 2, 2.2]], columns=["c", "a", "b"]) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
def test_insert_with_columns_dups(self): | ||
# GH#14291 | ||
df = DataFrame() | ||
df.insert(0, "A", ["g", "h", "i"], allow_duplicates=True) | ||
df.insert(0, "A", ["d", "e", "f"], allow_duplicates=True) | ||
df.insert(0, "A", ["a", "b", "c"], allow_duplicates=True) | ||
exp = DataFrame( | ||
[["a", "d", "g"], ["b", "e", "h"], ["c", "f", "i"]], columns=["A", "A", "A"] | ||
) | ||
tm.assert_frame_equal(df, exp) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
from datetime import timedelta | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
import pandas as pd | ||
from pandas import Index, Timedelta, TimedeltaIndex, timedelta_range | ||
import pandas._testing as tm | ||
|
||
|
||
class TestTimedeltaIndexInsert: | ||
def test_insert(self): | ||
|
||
idx = TimedeltaIndex(["4day", "1day", "2day"], name="idx") | ||
|
||
result = idx.insert(2, timedelta(days=5)) | ||
exp = TimedeltaIndex(["4day", "1day", "5day", "2day"], name="idx") | ||
tm.assert_index_equal(result, exp) | ||
|
||
# insertion of non-datetime should coerce to object index | ||
result = idx.insert(1, "inserted") | ||
expected = Index( | ||
[Timedelta("4day"), "inserted", Timedelta("1day"), Timedelta("2day")], | ||
name="idx", | ||
) | ||
assert not isinstance(result, TimedeltaIndex) | ||
tm.assert_index_equal(result, expected) | ||
assert result.name == expected.name | ||
|
||
idx = timedelta_range("1day 00:00:01", periods=3, freq="s", name="idx") | ||
|
||
# preserve freq | ||
expected_0 = TimedeltaIndex( | ||
["1day", "1day 00:00:01", "1day 00:00:02", "1day 00:00:03"], | ||
name="idx", | ||
freq="s", | ||
) | ||
expected_3 = TimedeltaIndex( | ||
["1day 00:00:01", "1day 00:00:02", "1day 00:00:03", "1day 00:00:04"], | ||
name="idx", | ||
freq="s", | ||
) | ||
|
||
# reset freq to None | ||
expected_1_nofreq = TimedeltaIndex( | ||
["1day 00:00:01", "1day 00:00:01", "1day 00:00:02", "1day 00:00:03"], | ||
name="idx", | ||
freq=None, | ||
) | ||
expected_3_nofreq = TimedeltaIndex( | ||
["1day 00:00:01", "1day 00:00:02", "1day 00:00:03", "1day 00:00:05"], | ||
name="idx", | ||
freq=None, | ||
) | ||
|
||
cases = [ | ||
(0, Timedelta("1day"), expected_0), | ||
(-3, Timedelta("1day"), expected_0), | ||
(3, Timedelta("1day 00:00:04"), expected_3), | ||
(1, Timedelta("1day 00:00:01"), expected_1_nofreq), | ||
(3, Timedelta("1day 00:00:05"), expected_3_nofreq), | ||
] | ||
|
||
for n, d, expected in cases: | ||
result = idx.insert(n, d) | ||
tm.assert_index_equal(result, expected) | ||
assert result.name == expected.name | ||
assert result.freq == expected.freq | ||
|
||
@pytest.mark.parametrize( | ||
"null", [None, np.nan, np.timedelta64("NaT"), pd.NaT, pd.NA] | ||
) | ||
def test_insert_nat(self, null): | ||
# GH 18295 (test missing) | ||
idx = timedelta_range("1day", "3day") | ||
result = idx.insert(1, null) | ||
expected = TimedeltaIndex(["1day", pd.NaT, "2day", "3day"]) | ||
tm.assert_index_equal(result, expected) | ||
|
||
def test_insert_invalid_na(self): | ||
idx = TimedeltaIndex(["4day", "1day", "2day"], name="idx") | ||
with pytest.raises(TypeError, match="incompatible label"): | ||
idx.insert(0, np.datetime64("NaT")) | ||
|
||
def test_insert_dont_cast_strings(self): | ||
# To match DatetimeIndex and PeriodIndex behavior, dont try to | ||
# parse strings to Timedelta | ||
idx = timedelta_range("1day", "3day") | ||
|
||
result = idx.insert(0, "1 Day") | ||
assert result.dtype == object | ||
assert result[0] == "1 Day" | ||
|
||
def test_insert_empty(self): | ||
# Corner case inserting with length zero doesnt raise IndexError | ||
idx = timedelta_range("1 Day", periods=3) | ||
td = idx[0] | ||
|
||
idx[:0].insert(0, td) | ||
idx[:0].insert(1, td) | ||
idx[:0].insert(-1, td) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.