Skip to content

Question / Possibly a Feature request: comparison machinery for unit tests #14221

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
rs2 opened this issue Sep 14, 2016 · 3 comments
Closed

Question / Possibly a Feature request: comparison machinery for unit tests #14221

rs2 opened this issue Sep 14, 2016 · 3 comments
Labels
Testing pandas testing functions or related to the test suite Usage Question

Comments

@rs2
Copy link
Contributor

rs2 commented Sep 14, 2016

When it comes to writing unit tests that return a DataFrame or a TimeSeries there's a clear need to compare the output to the expected result. The comparison can be strict, or relaxed in terms of the order of columns and/or order of rows, and could also be forgiving for floats.

I'm thinking of an approach implemented in https://pythonhosted.org/toytable/toytable.table.html#table-literals, i.e. defining a table as

>>> pokedex = table_literal("""
...     | Pokemon (str) | Level (int) | Owner (str) | Type (str) |
...     | Pikchu        | 12          | Ash Ketchum | Electric   |
...     | Bulbasaur     | 16          | Ash Ketchum | Grass      |
...     | Charmander    | 19          | Ash Ketchum | Fire       |
...     | Raichu        | 23          | Lt. Surge   | Electric   |
...     """)

and subsequently invoking self.assertTableEqual or self.assertTableContentsEqual.

Does anything similar exist in pandas (I could not find), and can the above approach be upvoted please?

@jreback
Copy link
Contributor

jreback commented Sep 14, 2016

well we have an entire module for this: https://github.com/pydata/pandas/blob/master/pandas/util/testing.py

not to mention that the entire test suite uses things like this.

try

from pandas .util import testing as tm
tm.assert_frame_equal

@jreback jreback closed this as completed Sep 14, 2016
@jreback jreback added Testing pandas testing functions or related to the test suite Usage Question labels Sep 14, 2016
@jreback jreback added this to the No action milestone Sep 14, 2016
@rs2
Copy link
Contributor Author

rs2 commented Sep 14, 2016

How would you build & visualize the expected DataFrame? The table_literal approach seems to be appropriate.

@jorisvandenbossche
Copy link
Member

jorisvandenbossche commented Sep 14, 2016

@rs2 Just the same as you otherwise build a small dataframe, eg pd.DataFrame(..) with eg a dictionary.
If you want to ahve something visual like the above, you can always pass such a string to read_csv or read_fwf

See also #9895 for proposal to make the testing functions more 'officially' public

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Testing pandas testing functions or related to the test suite Usage Question
Projects
None yet
Development

No branches or pull requests

3 participants