Skip to content
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

Allow pandas.DataFrame table and 1D/2D numpy array inputs into pygmt.info #574

Merged
merged 4 commits into from
Sep 7, 2020

Conversation

weiji14
Copy link
Member

@weiji14 weiji14 commented Sep 2, 2020

Description of proposed changes

Let pygmt.info support pandas.DataFrame and 1D/2D numpy array inputs. Enables code like so:

import pandas as pd

df: pd.DataFrame = pygmt.datasets.load_ocean_ridge_points()
region = pygmt.info(table=df, spacing="0.1")  # pygmt.info("@ridge.txt", I="0.1")
print(region)
# '-R-180/180/-65.7/86.8\n',

I've renamed the 'fname' argument to 'table' (which is a 💥 breaking change) since info supports both file name inputs and pandas.DataFrame tables now.

Fixes #564

Reminders

  • Run make format and make check to make sure the code follows the style guide.
  • Add tests for new features or tests that would have caught the bug that you're fixing.
  • Add new public functions/methods/classes to doc/api/index.rst.
  • Write detailed docstrings for all functions/methods.
  • If adding new functionality, add an example to docstrings or tutorials.

Also renamed 'fname' argument to 'table'
since `info` supports both file name inputs
and pandas.DataFrame tables now.
@weiji14 weiji14 added the enhancement Improving an existing feature label Sep 2, 2020
@weiji14 weiji14 marked this pull request as draft September 2, 2020 01:58
@weiji14 weiji14 marked this pull request as ready for review September 2, 2020 01:59
@weiji14 weiji14 mentioned this pull request Sep 2, 2020
5 tasks
@weiji14 weiji14 mentioned this pull request Sep 6, 2020
11 tasks
@weiji14 weiji14 changed the title Allow pandas.DataFrame table inputs into pygmt.info Allow pandas.DataFrame table and 1D/2D numpy array inputs into pygmt.info Sep 7, 2020
if kind == "file":
file_context = dummy_context(table)
elif kind == "matrix":
_table = np.asanyarray(table)
Copy link
Member Author

@weiji14 weiji14 Sep 7, 2020

Choose a reason for hiding this comment

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

Just FYI, this line converts pandas.DataFrame into a numpy.ndarray, while allowing numpy.ndarray objects to just pass through. A bit easier than using _table = table.values if hasattr(table, values) else table, and I think this allows for Python lists to work too (though I haven't tested it).

See also:

Copy link
Member

@seisman seisman left a comment

Choose a reason for hiding this comment

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

Looks good

@weiji14 weiji14 merged commit 53eb1b5 into master Sep 7, 2020
@weiji14 weiji14 deleted the info_on_tables branch September 7, 2020 05:16
@weiji14 weiji14 added this to the 0.2.x milestone Sep 8, 2020
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improving an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Let pygmt.info support pandas.DataFrame or 2D numpy.array inputs
2 participants