Skip to content

Commit d9df659

Browse files
authored
Allow pandas.Series inputs to fig.histogram and pygmt.info (#1329)
Let 1D pandas.Series inputs work properly by modifying the virtualfile_from_data function. Also added two tests in test_histogram.py and test_info.py to ensure this works.
1 parent 7f37e1c commit d9df659

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

pygmt/clib/session.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1449,9 +1449,11 @@ def virtualfile_from_data(
14491449
_data.extend(extra_arrays)
14501450
elif kind == "matrix": # turn 2D arrays into list of vectors
14511451
try:
1452+
# pandas.Series will be handled below like a 1d numpy ndarray
1453+
assert not hasattr(data, "to_frame")
14521454
# pandas.DataFrame and xarray.Dataset types
14531455
_data = [array for _, array in data.items()]
1454-
except AttributeError:
1456+
except (AttributeError, AssertionError):
14551457
try:
14561458
# Just use virtualfile_from_matrix for 2D numpy.ndarray
14571459
# which are signed integer (i), unsigned integer (u) or
@@ -1460,7 +1462,7 @@ def virtualfile_from_data(
14601462
_virtualfile_from = self.virtualfile_from_matrix
14611463
_data = (data,)
14621464
except (AssertionError, AttributeError):
1463-
# Python lists, tuples, and numpy ndarray types
1465+
# Python list, tuple, numpy ndarray and pandas.Series types
14641466
_data = np.atleast_2d(np.asanyarray(data).T)
14651467

14661468
# Finally create the virtualfile from the data, to be passed into GMT

pygmt/tests/test_histogram.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@
22
"""
33
Tests histogram.
44
"""
5+
import pandas as pd
56
import pytest
67
from pygmt import Figure
78

89

9-
@pytest.fixture(scope="module")
10-
def table():
10+
@pytest.fixture(scope="module", name="table", params=[list, pd.Series])
11+
def fixture_table(request):
1112
"""
1213
Returns a list of integers to be used in the histogram.
1314
"""
14-
return [1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 4, 5, 6, 7, 8, 8, 8, 8, 8, 8]
15+
data = [1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 4, 5, 6, 7, 8, 8, 8, 8, 8, 8]
16+
return request.param(data)
1517

1618

17-
@pytest.mark.mpl_image_compare
19+
@pytest.mark.mpl_image_compare(filename="test_histogram.png")
1820
def test_histogram(table):
1921
"""
20-
Tests plotting a histogram using a list of integers.
22+
Tests plotting a histogram using a sequence of integers from a table.
2123
"""
2224
fig = Figure()
2325
fig.histogram(

pygmt/tests/test_info.py

+9
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ def test_info_2d_list():
3838
assert output == expected_output
3939

4040

41+
def test_info_series():
42+
"""
43+
Make sure info works on a pandas.Series input.
44+
"""
45+
output = info(pd.Series(data=[0, 4, 2, 8, 6]))
46+
expected_output = "<vector memory>: N = 5 <0/8>\n"
47+
assert output == expected_output
48+
49+
4150
def test_info_dataframe():
4251
"""
4352
Make sure info works on pandas.DataFrame inputs.

0 commit comments

Comments
 (0)