Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pipe operator doesn't work in plain python prompt #45

Closed
supersaw-it opened this issue Aug 3, 2021 · 9 comments
Closed

pipe operator doesn't work in plain python prompt #45

supersaw-it opened this issue Aug 3, 2021 · 9 comments
Labels
raw python repl Issues with piping syntax in raw python REPL

Comments

@supersaw-it
Copy link

supersaw-it commented Aug 3, 2021

seems that the pipe operator doesn't work when using datar in virtual anaconda environments.
here's a snippet of running the example code ran in anaconda prompt:

from datar.all import f, mutate, filter, if_else, tibble

[2021-08-03 19:57:12][datar][WARNING] Builtin name "filter" has been overriden by datar.

df = tibble(
    x=range(4),
    y=['zero', 'one', 'two', 'three']
)

C:\ProgramData\Anaconda3\envs\conda_start\lib\site-packages\pipda\utils.py:159: UserWarning: Failed to fetch the node calling the function, call it with the original function.
warnings.warn(

df >> mutate(z=f.x)

C:\ProgramData\Anaconda3\envs\conda_start\lib\site-packages\pipda\utils.py:159: UserWarning: Failed to fetch the node calling the function, call it with the original function.
warnings.warn(
Traceback (most recent call last):
File "", line 1, in
File "C:\ProgramData\Anaconda3\envs\conda_start\lib\site-packages\pipda\register.py", line 396, in wrapper
return calling_rule(generic, args, kwargs, envdata)
File "C:\ProgramData\Anaconda3\envs\conda_start\lib\site-packages\pipda_calling.py", line 93, in verb_calling_rule3
return generic(*args, **kwargs)
File "C:\ProgramData\Anaconda3\envs\conda_start\lib\functools.py", line 872, in wrapper
raise TypeError(f'{funcname} requires at least '
TypeError: _not_implemented requires at least 1 positional argument

mutate(df, z=f.x)

>         x        y       z
>   <int64> <object> <int64>
> 0       0     zero       0
> 1       1      one       1
> 2       2      two       2
> 3       3    three       3

pandas 1.2.3
python 3.8.1

ps thanks a lot for the package, hopefully the issue can be closed soon :)

@pwwang
Copy link
Owner

pwwang commented Aug 3, 2021

What's the OS? Windows? Linux?
What's the python prompt? python? ipython? bpython?
What's the version of datar? import datar; print(datar.__version__)

@supersaw-it
Copy link
Author

supersaw-it commented Aug 3, 2021

What's the OS? Windows? Linux?

windows 10

What's the python prompt? python? ipython? bpython?

anaconda prompt (anaconda3) - python.exe 3.8.1

What's the version of datar? import datar; print(datar.__version__)

0.4.2

@pwwang
Copy link
Owner

pwwang commented Aug 3, 2021

pipda, the backend of datar to implement the piping syntax, relies on the source code to find the AST node to detect the environment of the function to be called (whether it's in piping syntax or regular function calling).

However, the source code is not available when the code is running in plain python prompt, thus datar couldn't tell if a verb is called in piping syntax.

There are several solutions for this:

  1. Use a 3-rd party prompt (ipython, bpython) to run the code
  2. Run the code in as a script file (python script.py)
  3. Use a regular way to call the verbs (mutate(df, z=f.x))

@pwwang pwwang changed the title pipe operator doesn't work in anaconda envs pipe operator doesn't work in plain python prompt Aug 3, 2021
@supersaw-it
Copy link
Author

I'm using PyCharm with Anaconda + plain python prompt, will try switching to ipython then;

thanks so much for your quick answers!

@pwwang
Copy link
Owner

pwwang commented Aug 3, 2021

You are welcome! Thank you for reporting!

pipda (v0.4.4) has a new mode pipda.options.assume_all_piping = True, which handles the case where source code is not available. With this mode, the verbs are assumed to be called using piping syntax.

datar will adopt this mode in the next version.

@pwwang
Copy link
Owner

pwwang commented Aug 4, 2021

With this revision 9bd9d02, one could use the "all piping" mode (all verbs are supposed to be used in piping syntax):

from datar.all import f, mutate, filter, if_else, tibble
from pipda import options

options.assume_all_piping = True

df = tibble(
    x=range(4),
    y=['zero', 'one', 'two', 'three']
)

df >> mutate(z=f.x)
# However, without source code available, we can't have both of the above and below work:
# mutate(df, z=f.x)

@supersaw-it
Copy link
Author

supersaw-it commented Aug 4, 2021

thanks a lot for making it all clear!

is there a way to pip install the dev branch of the module to already have the 9bd9d02 revision?

I'm new to github

The question is simply out of curiousity, everything works for me now that I installed ipython 🍪

@pwwang
Copy link
Owner

pwwang commented Aug 4, 2021

You can do:

pip install git+git://github.com/pwwang/datar.git@9bd9d0253a3f313e0cf5ce0fb04b6192573bb11e

But I don't recommend it. It's still in development and may need more tests.

@pwwang pwwang mentioned this issue Aug 8, 2021
pwwang added a commit that referenced this issue Aug 9, 2021
* Make sure assume_all_piping mode works internally: #45

* Add ToothGrowth dataset

* Change datasets.all_datasets() to datasets.list_datasets()

* Make dataset names case-insensitive; add datasets economics, economics_long, faithful, faithfuld, luv_colours, midwest, mpg, msleep, presidential, seals, txhousing

* 0.4.3

* Add `base.complete_cases`
@pwwang
Copy link
Owner

pwwang commented Aug 9, 2021

Closeing. As 0.4.3 is now able run the assume_all_piping mode.
See: #45 (comment)

@pwwang pwwang closed this as completed Aug 9, 2021
@pwwang pwwang mentioned this issue Sep 16, 2021
pwwang added a commit that referenced this issue Sep 16, 2021
* 📝 Add documentation for the "blind" environments (#45, #54, #55)

* 🩹 Fix trimws not importable from datar.all/datar.base

* ✨ Make as_date() return pd datetime types; Add as_pd_date() as an alias of pd.to_datetime() (#56)

* 🔖 0.5.1

* 🚨 Fix linting

* 👷 Deploy the docs on dev branch as well

* 💚 Fix docs deply in CI
@pwwang pwwang added the raw python repl Issues with piping syntax in raw python REPL label Dec 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
raw python repl Issues with piping syntax in raw python REPL
Projects
None yet
Development

No branches or pull requests

2 participants