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

dataframe.eval() can't work with 'cash' column ? #6139

Closed
halleygithub opened this issue Jan 28, 2014 · 5 comments
Closed

dataframe.eval() can't work with 'cash' column ? #6139

halleygithub opened this issue Jan 28, 2014 · 5 comments

Comments

@halleygithub
Copy link

>>> df
                    cash    sales  net_pft
STK_ID RPT_Date                           
600141 20120331   7.8427  21.0882   0.6560
       20120630   7.5140  46.9515   1.5912
       20120930   7.7667  74.0364   2.5173
       20121231  17.2350  96.1204   2.9458
       20130331  13.3643  25.0869   0.7267
       20130630  16.0832  56.6674   0.8088
       20130930  16.7898  86.3745   1.3081

[7 rows x 3 columns]
>>> df.dtypes
cash       float64
sales      float64
net_pft    float64
dtype: object
>>> df.eval('net_pft +  sales')
STK_ID  RPT_Date
600141  20120331    21.7442
        20120630    48.5427
        20120930    76.5537
        20121231    99.0662
        20130331    25.8136
        20130630    57.4762
        20130930    87.6826
dtype: float64
>>> df.eval('net_pft +  cash')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Python2.7\lib\site-packages\pandas\core\frame.py", line 1845, in eval
    return _eval(expr, **kwargs)
  File "D:\Python2.7\lib\site-packages\pandas\computation\eval.py", line 208, in eval
    ret = eng_inst.evaluate()
  File "D:\Python2.7\lib\site-packages\pandas\computation\engines.py", line 50, in evaluate
    self.pre_evaluate()
  File "D:\Python2.7\lib\site-packages\pandas\computation\engines.py", line 33, in pre_evaluate
    self.expr.check_name_clashes()
  File "D:\Python2.7\lib\site-packages\pandas\computation\expr.py", line 799, in check_name_clashes
    _check_disjoint_resolver_names(res_keys, lcl_keys, gbl_keys)
  File "D:\Python2.7\lib\site-packages\pandas\computation\expr.py", line 41, in _check_disjoint_resolver_names
    raise NameResolutionError(msg)
pandas.computation.common.NameResolutionError: resolvers and locals overlap on names ['cash']
>>> df.eval('sales +  cash')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Python2.7\lib\site-packages\pandas\core\frame.py", line 1845, in eval
    return _eval(expr, **kwargs)
  File "D:\Python2.7\lib\site-packages\pandas\computation\eval.py", line 208, in eval
    ret = eng_inst.evaluate()
  File "D:\Python2.7\lib\site-packages\pandas\computation\engines.py", line 50, in evaluate
    self.pre_evaluate()
  File "D:\Python2.7\lib\site-packages\pandas\computation\engines.py", line 33, in pre_evaluate
    self.expr.check_name_clashes()
  File "D:\Python2.7\lib\site-packages\pandas\computation\expr.py", line 799, in check_name_clashes
    _check_disjoint_resolver_names(res_keys, lcl_keys, gbl_keys)
  File "D:\Python2.7\lib\site-packages\pandas\computation\expr.py", line 41, in _check_disjoint_resolver_names
    raise NameResolutionError(msg)
pandas.computation.common.NameResolutionError: resolvers and locals overlap on names ['cash']
>>> pd.__version__
'0.13.0-203-g4518630'
>>> 

>>> df=df.rename(columns={'cash':'cas'})
>>> df
                     cas    sales  net_pft
STK_ID RPT_Date                           
600141 20120331   7.8427  21.0882   0.6560
       20120630   7.5140  46.9515   1.5912
       20120930   7.7667  74.0364   2.5173
       20121231  17.2350  96.1204   2.9458
       20130331  13.3643  25.0869   0.7267
       20130630  16.0832  56.6674   0.8088
       20130930  16.7898  86.3745   1.3081

[7 rows x 3 columns]
>>> df.eval('sales +  cas')
STK_ID  RPT_Date
600141  20120331     28.9309
        20120630     54.4655
        20120930     81.8031
        20121231    113.3554
        20130331     38.4512
        20130630     72.7506
        20130930    103.1643
dtype: float64
>>> df.eval('net_pft +  cas')
STK_ID  RPT_Date
600141  20120331     8.4987
        20120630     9.1052
        20120930    10.2840
        20121231    20.1808
        20130331    14.0910
        20130630    16.8920
        20130930    18.0979
dtype: float64
>>> 
@jreback
Copy link
Contributor

jreback commented Jan 28, 2014

the variable cash is defined in your sessino. do a del cash,then try again.

this is an API issue as it should just warn you and not raise (or optionally just ignore the error).

@cpcloud can you link the related issue

@halleygithub
Copy link
Author

Oh, yes. I do have a cash variable and df has a cash column. So if I don't want to del cash variable and want to add the two columns by df.eval('sales + cash'), what is the valid expression ?

@jreback
Copy link
Contributor

jreback commented Jan 28, 2014

del cash delete the name from the local name space

its raising because it sees a name conflict. as I said above we are going to change this to a warning (or actually for query this would just work as it will preference the column name).

@jorisvandenbossche
Copy link
Member

See #5987 for discussion on improving this. For now you will have to rename one of both I think.

@jreback
Copy link
Contributor

jreback commented Jan 28, 2014

closing this as already discussed in #5987

@jreback jreback closed this as completed Jan 28, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants