Skip to content

Validate that 'name' attribute is set only if hashable #9193

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
17 changes: 17 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
_default_index, _maybe_upcast,
_asarray_tuplesafe, _infer_dtype_from_scalar,
is_list_like, _values_from_object,
is_hashable,
_possibly_cast_to_datetime, _possibly_castable,
_possibly_convert_platform, _try_sort,
ABCSparseArray, _maybe_match_name, _coerce_to_dtype,
Expand Down Expand Up @@ -105,6 +106,8 @@ class Series(base.IndexOpsMixin, generic.NDFrame):
dict.
dtype : numpy.dtype or None
If None, dtype will be inferred
name : used to attach metadata to a Series, e.g., as str or namedtuple.
Must be hashable, defaults to None.
Copy link
Contributor

Choose a reason for hiding this comment

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

this is not accurate

copy : boolean, default False
Copy input data
"""
Expand Down Expand Up @@ -272,6 +275,20 @@ def _update_inplace(self, result, **kwargs):
# we want to call the generic version and not the IndexOpsMixin
return generic.NDFrame._update_inplace(self, result, **kwargs)

# Validate that name is hashable
@property
def name(self):
return self._name

@name.setter
def name(self, value):
if is_hashable(value):
Copy link
Contributor

Choose a reason for hiding this comment

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

do if value is not None here as it's the most common case

object.__setattr__(self, '_name', value)
else:
raise TypeError('Series.name must be hashable, got %s.'
% value.__class__.__name__)


# ndarray compatibility
@property
def dtype(self):
Expand Down
9 changes: 8 additions & 1 deletion pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,13 @@ def test_constructor_map(self):
result = Series(m, index=lrange(10, 20))
exp.index = lrange(10, 20)
assert_series_equal(result, exp)

def test_constructor_unhashable_name(self):
def set_to_unhashable(s_):
s_.name = {}
Copy link
Contributor

Choose a reason for hiding this comment

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

needs a test on the construcor as well

s = Series([1,3], name = 'test')
self.assertRaises(TypeError, set_to_unhashable, s)
self.assertEqual(s.name, 'test')

def test_constructor_categorical(self):
cat = pd.Categorical([0, 1, 2, 0, 1, 2], ['a', 'b', 'c'], fastpath=True)
Expand Down Expand Up @@ -1980,7 +1987,7 @@ def test_repr(self):
rep_str = repr(ser)
self.assertIn("Name: 0", rep_str)

ser = Series(["a\n\r\tb"], name=["a\n\r\td"], index=["a\n\r\tf"])
ser = Series(["a\n\r\tb"], name="a\n\r\td", index=["a\n\r\tf"])
self.assertFalse("\t" in repr(ser))
self.assertFalse("\r" in repr(ser))
self.assertFalse("a\n" in repr(ser))
Expand Down