diff --git a/src/metpy/io/metar.py b/src/metpy/io/metar.py index 31d3338cc41..c7d92fe8c55 100644 --- a/src/metpy/io/metar.py +++ b/src/metpy/io/metar.py @@ -477,7 +477,7 @@ def _metars_to_dataframe(metar_iter, *, year=None, month=None): # Set the units for the dataframe--filter out warning from Pandas with warnings.catch_warnings(): warnings.simplefilter('ignore', UserWarning) - df.units = col_units + df.units = col_units.copy() return df diff --git a/src/metpy/plots/station_plot.py b/src/metpy/plots/station_plot.py index cb71fb267ef..6bacd55ffd1 100644 --- a/src/metpy/plots/station_plot.py +++ b/src/metpy/plots/station_plot.py @@ -163,7 +163,7 @@ def plot_symbols(mapper, name, nwrap=10, figsize=(10, 1.4)): kwargs['fontproperties'] = wx_symbol_font.copy() return self.plot_parameter(location, codes, symbol_mapper, **kwargs) - def plot_parameter(self, location, parameter, formatter='.0f', **kwargs): + def plot_parameter(self, location, parameter, formatter=None, **kwargs): """At the specified location in the station model plot a set of values. This specifies that at the offset `location`, the data in `parameter` should be @@ -186,7 +186,7 @@ def plot_parameter(self, location, parameter, formatter='.0f', **kwargs): formatter : str or Callable, optional How to format the data as a string for plotting. If a string, it should be compatible with the :func:`format` builtin. If a callable, this should take a - value and return a string. Defaults to '0.f'. + value and return a string. Defaults to 'z0.f'. plot_units: `pint.unit` Units to plot in (performing conversion if necessary). Defaults to given units. kwargs @@ -372,6 +372,14 @@ def _make_kwargs(self, kwargs): @staticmethod def _to_string_list(vals, fmt): """Convert a sequence of values to a list of strings.""" + if fmt is None: + import sys + if sys.version_info >= (3, 11): + fmt = 'z.0f' + else: + def fmt(s): + """Perform default formatting with no decimal places and no negative 0.""" + return format(round(s, 0) + 0., '.0f') if not callable(fmt): def formatter(s): """Turn a format string into a callable.""" diff --git a/tests/io/test_metar.py b/tests/io/test_metar.py index 55f2a290d1d..6ed6d170460 100644 --- a/tests/io/test_metar.py +++ b/tests/io/test_metar.py @@ -399,3 +399,12 @@ def test_repr(): "dewp=TreeNode(text='M10', offset=38)), " "altim=TreeNode(text=' A3026', offset=41), " "remarks=TreeNode(text='', offset=47), end=TreeNode(text='', offset=47))") + + +def test_metar_units_in_place(): + """Test that parsing a METAR yields units that can be changed safely in-place.""" + df = parse_metar_to_dataframe('KDEN 012153Z 09010KT 10SM FEW060 BKN110 BKN220 27/13 A3010') + df.units['air_temperature'] = 'degF' + + df = parse_metar_to_dataframe('KDEN 012153Z 09010KT 10SM FEW060 BKN110 BKN220 27/13 A3010') + assert df.units['air_temperature'] == 'degC' diff --git a/tests/plots/baseline/test_declarative_sfc_obs.png b/tests/plots/baseline/test_declarative_sfc_obs.png index e7542c49950..1ccac8450ed 100644 Binary files a/tests/plots/baseline/test_declarative_sfc_obs.png and b/tests/plots/baseline/test_declarative_sfc_obs.png differ diff --git a/tests/plots/baseline/test_declarative_sfc_obs_args.png b/tests/plots/baseline/test_declarative_sfc_obs_args.png index 0237f1addbd..b4b74e9df70 100644 Binary files a/tests/plots/baseline/test_declarative_sfc_obs_args.png and b/tests/plots/baseline/test_declarative_sfc_obs_args.png differ diff --git a/tests/plots/baseline/test_declarative_sfc_obs_changes.png b/tests/plots/baseline/test_declarative_sfc_obs_changes.png index 21025f59ceb..37434466137 100644 Binary files a/tests/plots/baseline/test_declarative_sfc_obs_changes.png and b/tests/plots/baseline/test_declarative_sfc_obs_changes.png differ diff --git a/tests/plots/baseline/test_declarative_sfc_obs_full.png b/tests/plots/baseline/test_declarative_sfc_obs_full.png index 157e7a1da47..4f376ee0f46 100644 Binary files a/tests/plots/baseline/test_declarative_sfc_obs_full.png and b/tests/plots/baseline/test_declarative_sfc_obs_full.png differ diff --git a/tests/plots/test_station_plot.py b/tests/plots/test_station_plot.py index 0a784207aa4..80012bc9ab0 100644 --- a/tests/plots/test_station_plot.py +++ b/tests/plots/test_station_plot.py @@ -101,6 +101,18 @@ def test_station_plot_locations(): return fig +def test_station_plot_negative_zero(): + """Test that we avoid formatting a negative 0 by default.""" + fig = plt.figure() + ax = fig.add_subplot(1, 1, 1) + sp = StationPlot(ax, [0], [0]) + text = sp.plot_parameter('C', [-0.04]) + + assert text.text[0] == '0' + + plt.close(fig) + + @pytest.mark.mpl_image_compare(tolerance=0.00413, savefig_kwargs={'dpi': 300}, remove_text=True) def test_stationlayout_api():