Skip to content

Commit

Permalink
Fix parameters to ggplot object
Browse files Browse the repository at this point in the history
ggplot(data=df) resulted in an exception.
  • Loading branch information
has2k1 committed Jun 21, 2017
1 parent c64385c commit 0957b35
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ v0.2.1

- Fixed bug in :class:`~plotnine.stat_summary` where the you got
an exception for some types of the `x` aesthetic values.

- Fixed bug where ``ggplot(data=df)`` resulted in an exception.


v0.2.0
Expand Down
3 changes: 2 additions & 1 deletion plotnine/ggplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class ggplot(object):

def __init__(self, mapping=None, data=None, environment=None):
# Allow some sloppiness
if not isinstance(mapping, aes):
if (isinstance(mapping, pd.DataFrame) and
(data is None or isinstance(data, aes))):
mapping, data = data, mapping
if mapping is None:
mapping = aes()
Expand Down
17 changes: 17 additions & 0 deletions plotnine/tests/test_ggplot_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,28 @@ def test_labels():

def test_ggplot_parameters():
p = ggplot(df, aes('x'))

assert p.data is df
assert p.mapping == aes('x')
assert p.environment.namespace['np'] is np
assert p.environment.namespace['pd'] is pd

p = ggplot(data=df, mapping=aes('x'))
assert p.data is df
assert p.mapping == aes('x')

p = ggplot(data=df)
assert p.data is df
assert p.mapping == aes()

p = ggplot(mapping=aes('x'))
assert p.data is None
assert p.mapping == aes('x')

p = ggplot()
assert p.data is None
assert p.mapping == aes()

with pytest.raises(PlotnineError):
ggplot([1, 2, 3], aes('x'))

Expand Down

0 comments on commit 0957b35

Please sign in to comment.