Skip to content

TST: Add test cases for GH6173, appending to empty df #23806

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 13 commits into from
Nov 23, 2018
Merged
Show file tree
Hide file tree
Changes from 10 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
32 changes: 32 additions & 0 deletions pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,3 +804,35 @@ def test_loc_uint64(self):
result = s.loc[[np.iinfo('uint64').max - 1,
np.iinfo('uint64').max]]
tm.assert_series_equal(result, s)

def test_loc_setitem_empty_append(self):
# GH6173, various appends to an empty dataframe

data = [1, 2, 3]
expected = DataFrame({'x': data, 'y': [None] * len(data)})

# appends to fit length of data
df = DataFrame(columns=['x', 'y'])
df.loc[:, 'x'] = data
tm.assert_frame_equal(df, expected)

# only appends one value
expected = DataFrame({'x': [1.0], 'y': [np.nan]})
df = DataFrame(columns=['x', 'y'],
dtype=np.float)
df.loc[0, 'x'] = expected.loc[0, 'x']
tm.assert_frame_equal(df, expected)

def test_loc_setitem_empty_append_raises(self):
# GH6173, various appends to an empty dataframe

data = [1, 2]
df = DataFrame(columns=['x', 'y'])
msg = (r"None of \[Int64Index\(\[0, 1\], dtype='int64'\)\] "
r"are in the \[index\]")
with pytest.raises(KeyError, match=msg):
df.loc[[0, 1], 'x'] = data

msg = "cannot copy sequence with size 2 to array axis with dimension 0"
with pytest.raises(ValueError, match=msg):
df.loc[0:2, 'x'] = data
21 changes: 20 additions & 1 deletion pandas/tests/indexing/test_multiindex.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
from warnings import catch_warnings

import numpy as np
Expand All @@ -7,7 +8,8 @@

import pandas as pd
from pandas import (
DataFrame, Index, MultiIndex, Panel, Series, Timestamp, date_range)
DataFrame, Index, MultiIndex, Panel, Period, Series, Timestamp, date_range,
period_range)
from pandas.tests.indexing.common import _mklbl
from pandas.util import testing as tm

Expand Down Expand Up @@ -1340,3 +1342,20 @@ def test_panel_setitem_with_multiindex(self):
p5.iloc[0, :, 0] = [1, 2]
expected = Panel(arr, **axes)
tm.assert_panel_equal(p5, expected)


def test_multiindex_period_datetime():
# GH4861, using datetime in period of multiindex raises exception
Copy link
Contributor

Choose a reason for hiding this comment

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

did you duplicate this test? it is already in master

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't explicitly add it again, it was just showing up in the diff for some reason. I thought I was completely up-to-date with master. Anyway, I synched again, and I think things are now in a good state because it is no longer showing in the diff.


idx1 = Index(['a', 'a', 'a', 'b', 'b'])
idx2 = period_range('2012-01', periods=len(idx1), freq='M')
s = Series(np.random.randn(len(idx1)), [idx1, idx2])

# try Period as index
expected = s.iloc[0]
result = s.loc['a', Period('2012-01')]
assert result == expected

# try datetime as index
result = s.loc['a', datetime(2012, 1, 1)]
assert result == expected