Skip to content

Support datetime types in region argument #561

Closed

Description

@weiji14

Description of the desired feature

Support passing in 'datetime' like inputs into the 'region' or '-R' argument in plot.

  • The region argument doesn't work with numpy datetime64 objects, have to convert to a string using np.datetime_as_string in order to set the map frame bounds.

The decorator kwargs_to_strings convert the list [w, e, s, n] to a string w/e/s/n. We need to convert datetime64 object to string internally, but it's not ideal to do the conversion in the decorator. I don't have a good solution now.

Originally posted by @seisman in #464 (comment)

Minimal working example:

import pandas as pd
import pygmt

fig = pygmt.Figure()
fig.plot(x=["2020-06-30"], y=[1], region=[pd.Timestamp("2020-01-01"), pd.Timestamp("2020-12-31"), 0, 2])
fig.show()

The error message is:

GMTCLibError: Module 'plot' failed with status code 71:
plot [ERROR]: Option -R parsing failure. Correct syntax:
plot [ERROR]: Offending option -R2020-01-01

This workaround does work, but it's cumbersome:

fig = pygmt.Figure()
fig.plot(
    x=[pd.Timestamp("2020-06-30")],
    y=[1],
    region=[
        pd.Timestamp("2020-01-01").isoformat(),
        pd.Timestamp("2020-12-31").isoformat(),
        0,
        2,
    ],
    style="a10c",
    frame=True,
)
fig.show()

produces:

Expected plot with star in middle

Are you willing to help implement and maintain this feature? Yes, but where should this code logic go?

Activity

  1. added
    enhancement
    on Aug 13, 2020
  2. seisman commented on Aug 13, 2020

    @seisman
    Member

    Add a check in the kwargs_to_strings decorator?

    def kwargs_to_strings(convert_bools=True, **conversions):

  3. weiji14 commented on Aug 13, 2020

    @weiji14
    MemberAuthor

    I'm wondering if this functionality is specific to plot only, or if there are other places which can take datetime-like 'region' arguments.

  4. seisman commented on Aug 14, 2020

    @seisman
    Member

    Almost any modules can accept datetime-like region, for example, basemap, histogram, and even grdimage (maybe, but never tried).

  5. weiji14 commented on Aug 14, 2020

    @weiji14
    MemberAuthor

    Ok, the extra 'datetime' check might slow things down by a bit, but since it's only checking for 4 (or 6) items in a list, it should be fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions