From ecdb66cb904726b259da151374421b5c915e43a9 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Sun, 13 Nov 2022 09:15:09 +0800 Subject: [PATCH] meca: Fix the bug when passing a dict of scalar values to the spec parameter (#2174) Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com> --- pygmt/src/meca.py | 4 ++++ pygmt/tests/test_meca.py | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/pygmt/src/meca.py b/pygmt/src/meca.py index d8d15156f9d..ef7dc61890a 100644 --- a/pygmt/src/meca.py +++ b/pygmt/src/meca.py @@ -283,6 +283,10 @@ def meca( # convert dict to pd.DataFrame so columns can be reordered if isinstance(spec, dict): + # convert values to ndarray so pandas doesn't complain about "all + # scalar values". See + # https://github.com/GenericMappingTools/pygmt/pull/2174 + spec = {key: np.atleast_1d(value) for key, value in spec.items()} spec = pd.DataFrame(spec) # expected columns are: diff --git a/pygmt/tests/test_meca.py b/pygmt/tests/test_meca.py index 3783ce2eece..ea7486079a2 100644 --- a/pygmt/tests/test_meca.py +++ b/pygmt/tests/test_meca.py @@ -291,3 +291,29 @@ def test_meca_dict_offset_eventname(): event_name="Event20220311", ) return fig + + +@pytest.mark.mpl_image_compare(filename="test_meca_dict_eventname.png") +def test_meca_spec_dict_all_scalars(): + """ + Test supplying a dict with scalar values for all focal parameters. + + This is a regression test for + https://github.com/GenericMappingTools/pygmt/pull/2174 + """ + fig = Figure() + fig.basemap(region=[-125, -122, 47, 49], projection="M6c", frame=True) + fig.meca( + spec=dict( + strike=330, + dip=30, + rake=90, + magnitude=3, + longitude=-124, + latitude=48, + depth=12.0, + event_name="Event20220311", + ), + scale="1c", + ) + return fig