1

I am using Pandas(python).I want to write this line

a=pd.Series([1,2,3])

But bymistake I write it as

a=pd.Series=[1,2,3]

When I write correct syntax it gives error

ba=pd.Series([1,2,3])

TypeError: 'list' object is not callable

Because it assign list pd.Series =[1, 2, 3]

>>> pd.Series
[1, 2, 3]

How to remove this list from pd.Series?

2 Answers2

4

You can simple re-assign pd.Series to the correct value:

pd.Series = pd.core.series.Series

This only works if there is a reference to the original function/class still around (in this case there is pd.core.series.Series). In case you overwrite something that isn't references somewhere else this won't work.

Just to give another example (based on a question in the comments), suppose you overwrote list, you can use the builtins module (or the __builtin__ module in Python 2):

list = [1,2,3]          # overwritten list
b = list(range(10))     # throws a "TypeError: 'list' object is not callable"

import builtins
list = builtins.list    # restored
b = list(range(10))     # works

However often it's much easier to restart the Python interpreter (which should also work) and in some cases it might only be the only option (in case nothing else references the overwridden value.

MSeifert
  • 145,886
  • 38
  • 333
  • 352
  • For me this problem happens with built-in `list`. What can I do? `list = list`? what do you think? – jezrael Jan 06 '18 at 09:11
  • `list = builtins.list` (after importing the [builtins module](https://docs.python.org/3/library/builtins.html)). – MSeifert Jan 06 '18 at 09:15
  • @jezrael you could see this question https://stackoverflow.com/questions/46263897/how-to-reassign-dict-functionality-back-to-the-dict – Bharath M Shetty Jan 06 '18 at 09:29
2

Use the reload buildin function to reload the module.

reload(pd)

Showcase:

>>> import pandas as pd
>>> pd.Series
<class 'pandas.core.series.Series'>
>>> pd.Series=[1,2,3]
>>> pd.Series
[1, 2, 3]
>>> reload(pd)
<module 'pandas' from '.local/lib/python2.7/site-packages/pandas/__init__.pyc'>
>>> pd.Series
<class 'pandas.core.series.Series'>
agtoever
  • 1,669
  • 16
  • 22
  • 1
    `reload` is python 2 only, so this won't work on Python 3 (and the question has a Python 3 tag). However you can use importlib (or similar) for an equivalent function. – MSeifert Jan 06 '18 at 09:31
  • You are right. I missed that. [importlib.reload](https://stackoverflow.com/questions/961162/reloading-module-giving-nameerror-name-reload-is-not-defined) is indeed the python3 equivalent. – agtoever Jan 06 '18 at 09:36