Skip to content

TST: Test for the Enum triggering TypeError (#22551 issue) #47715

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 20 commits into from
Jul 16, 2022
Merged
Changes from 5 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
15 changes: 15 additions & 0 deletions pandas/tests/frame/indexing/test_get_value.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from enum import Enum

import pytest

from pandas import (
Expand All @@ -20,3 +22,16 @@ def test_get_value(self, float_frame):
result = float_frame._get_value(idx, col)
expected = float_frame[col][idx]
assert result == expected


def test_enum_value():
Copy link
Member

Choose a reason for hiding this comment

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

  1. Could you name this test test_enum_column_equality
  2. Could you move this to pandas/tests/frame/test_arithmetic.py?
  3. Also below could you structure the test like
result = q1[Cols.col1] == q2[Cols.col1]
expected = ...
tm.assert_equal(result, expected)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure! Thank you for your guide.

Copy link
Contributor Author

@Shadimrad Shadimrad Jul 15, 2022

Choose a reason for hiding this comment

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

@mroeschke by result = q1[Cols.col1] == q2[Cols.col1] do you mean that I remove the .all() method as well?

Copy link
Member

Choose a reason for hiding this comment

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

Correct

Copy link
Member

Choose a reason for hiding this comment

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

The changes in this file can be removed now.

Copy link
Member

@mroeschke mroeschke Jul 15, 2022

Choose a reason for hiding this comment

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

If you inspect result, it's a Series

In [1]:  from enum import Enum

In [2]:     Cols = Enum("Cols", "col1 col2")
   ...:
   ...:     q1 = DataFrame({Cols.col1: [1, 2, 3]})
   ...:     q2 = DataFrame({Cols.col1: [1, 2, 3]})
   ...:
   ...:     result = q1[Cols.col1] == q2[Cols.col1]

In [3]: result
Out[3]:
0    True
1    True
2    True
Name: Cols.col1, dtype: bool

So expected should also be a Series

Copy link
Contributor Author

@Shadimrad Shadimrad Jul 15, 2022

Choose a reason for hiding this comment

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

@mroeschke
Right. but when I created the series and tried to compare,

data = np.array([True, True, True])
expected = pd.Series(data)

checking if expected == result gave me the Error that the truth value of the Series is ambiguous, that is why. Unless tm.assert_equal accounts for that. update: it did not.

Copy link
Member

Choose a reason for hiding this comment

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

Yup, tm.assert_equal will check the values and the aspects of the Series

In [1]:  from enum import Enum

In [2]:     Cols = Enum("Cols", "col1 col2")
   ...:
   ...:     q1 = DataFrame({Cols.col1: [1, 2, 3]})
   ...:     q2 = DataFrame({Cols.col1: [1, 2, 3]})
   ...:
   ...:     result = q1[Cols.col1] == q2[Cols.col1]

In [3]: result
Out[3]:
0    True
1    True
2    True
Name: Cols.col1, dtype: bool

In [4]: import pandas._testing as tm

In [5]: epxected = pd.Series([True, True, True], name=Cols.col1)

In [6]: tm.assert_series_equal(result, epxected)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Update: changing it to tm.assert_series_equal(result, expected) , hoping it works.

Copy link
Contributor Author

@Shadimrad Shadimrad Jul 15, 2022

Choose a reason for hiding this comment

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

Oh, I've just seen your reply. There was a syntax issue that I fixed. pd.Series now replaced with Series which hopefully should be working now. Thank you for your guidance as well.

Cols = Enum("Cols", "col1 col2")

q1 = DataFrame({Cols.col1: [1, 2, 3]})
q2 = DataFrame({Cols.col1: [1, 2, 3]})

result = (q1[Cols.col1] == q2[Cols.col1]).all()

expected = True

assert result == expected