Skip to content

Commit

Permalink
Merge pull request #1339 from CartoDB/fix-cdf-plot
Browse files Browse the repository at this point in the history
Fix CDF plot
  • Loading branch information
Jesus89 committed Dec 12, 2019
2 parents 360e4db + bc87560 commit bde9554
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
8 changes: 7 additions & 1 deletion cartoframes/core/cartodataframe.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from numpy import ndarray
from pandas import Series
from pandas import DataFrame, Series
from geopandas import GeoDataFrame, points_from_xy

from ..utils.geom_utils import decode_geometry_column
Expand Down Expand Up @@ -354,3 +354,9 @@ def explode(self, *args, **kwargs):
result = super(self._constructor, self).explode(*args, **kwargs)
result.__class__ = self._constructor
return result

def plot(self, *args, **kwargs):
if self.has_geometry():
return GeoDataFrame.plot(self, *args, **kwargs)
else:
return DataFrame(self).plot(*args, **kwargs)
2 changes: 2 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def test_simple_mocking(mocker):
mock_db_service.assert_called_with('foo')
```

NOTE: avoid `assert_called_once` because it does not work in Python 3.5.

**Classes**

```py
Expand Down
28 changes: 27 additions & 1 deletion tests/unit/core/test_cartodataframe.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Unit tests for cartoframes.data."""

from pandas import concat
from geopandas import GeoDataFrame
from pandas import DataFrame, Series
from geopandas import GeoDataFrame, GeoSeries
from shapely.geometry import box, Point

from cartoframes import CartoDataFrame
Expand Down Expand Up @@ -128,3 +129,28 @@ def test_concat(self):
cdf2 = CartoDataFrame({'a': [2], 'geometry': [Point(1, 1)]})
cdf = concat([cdf1, cdf2])
assert isinstance(cdf, CartoDataFrame)

def test_plot_series(self, mocker):
series_plot_mock = mocker.patch.object(Series, 'plot')
cdf = CartoDataFrame({'x': [1]})
cdf['x'].plot()
series_plot_mock.assert_called_once_with()

def test_plot_geoseries(self, mocker):
geoseries_plot_mock = mocker.patch.object(GeoSeries, 'plot')
cdf = CartoDataFrame({'geometry': [Point(0, 0)]})
cdf['geometry'].plot()
geoseries_plot_mock.assert_called_once_with()

def test_plot_dataframe(self, mocker):
dataframe_plot_mock = mocker.patch.object(DataFrame, 'plot')
cdf = CartoDataFrame({'x': [1], 'y': [1]})
cdf[['x', 'y']].plot()
dataframe_plot_mock.assert_called_once_with()

def test_plot_geodataframe(self, mocker):
geodataframe_plot_mock = mocker.patch.object(GeoDataFrame, 'plot')
cdf = CartoDataFrame({'x': [1], 'geometry': [Point(0, 0)]})
subset = cdf[['x', 'geometry']]
subset.plot(column='x')
geodataframe_plot_mock.assert_called_once_with(subset, column='x')

0 comments on commit bde9554

Please sign in to comment.