df.insert() is not returning a dataframe. Just change this line:
df = df.insert(0,thing.Date)
to:
df.insert(0,thing.Date)
df.insert() is not returning a dataframe. Just change this line:
df = df.insert(0,thing.Date)
to:
df.insert(0,thing.Date)
So a few things that need to be changed/fixed:
- Syntax for your
df = df.insert(0,thing.Date)needs to change todf.insert(0,thing.Date) - Your
def Date():function doesn't actually do anything in the code. A print statement will output to the console so you can visually see what it's doing. But right now it's not returning anything usable when you do your insert. Fix that by adding the following:
def Date(self): print ("2010-01") return "2010-01" - Finally, the last issue with your code is the fact that you are trying to use the pandas package function .to_excel() on a list. The list df doesn't have a
'.to_excel' function that can run on it. You have to use a Pandas dataframe object and to do that we have to convert your list into a dataframe like so:
pd.DataFrame(df).to_excel("C:\\file2.xls")
So finally your code will look like this:
import os
import pandas as pd
df = []
for f in ['C:\\file.xls']:
data = pd.read_excel(f, 'dspPrintOrExcel')
data.index = [os.path.basename(f)]*len(data)
class Column(object):
def Date(self):
print ("2010-01") # I kept your print in case you still wanted it
return "2010-01"
thing = Column()
df.insert(0,thing.Date())
pd.DataFrame(df).to_excel("C:\\file2.xls")
python - AttributeError: 'Index' object has no attribute 'to_excel' - Stack Overflow
python - Pandas 'function' object has no attribute 'to_excel' - Stack Overflow
excel - New to Python and can't figure out "AttributeError: 'NoneType' object has no attribute 'to_excel'" - Stack Overflow
Sporadic 'NoneType' object has no attribute error
NewFileName = input('Name your file: ')
df.to_excel(NewFileName, ".xlsx", index=False)Traceback (most recent call last): File "Rename.py", line 13, in <module>
df.to_excel(NewFileName, ".xlsx", index=False)
AttributeError: 'NoneType' object has no attribute 'to_excel'
df.insert() is not returning a dataframe. Just change this line:
df = df.insert(0,thing.Date)
to:
df.insert(0,thing.Date)
So a few things that need to be changed/fixed:
- Syntax for your
df = df.insert(0,thing.Date)needs to change todf.insert(0,thing.Date) - Your
def Date():function doesn't actually do anything in the code. A print statement will output to the console so you can visually see what it's doing. But right now it's not returning anything usable when you do your insert. Fix that by adding the following:
def Date(self): print ("2010-01") return "2010-01" - Finally, the last issue with your code is the fact that you are trying to use the pandas package function .to_excel() on a list. The list df doesn't have a
'.to_excel' function that can run on it. You have to use a Pandas dataframe object and to do that we have to convert your list into a dataframe like so:
pd.DataFrame(df).to_excel("C:\\file2.xls")
So finally your code will look like this:
import os
import pandas as pd
df = []
for f in ['C:\\file.xls']:
data = pd.read_excel(f, 'dspPrintOrExcel')
data.index = [os.path.basename(f)]*len(data)
class Column(object):
def Date(self):
print ("2010-01") # I kept your print in case you still wanted it
return "2010-01"
thing = Column()
df.insert(0,thing.Date())
pd.DataFrame(df).to_excel("C:\\file2.xls")
NoneType means that instead of an instance of whatever Class or Object you think you're working with, you've actually got None. That usually means that an assignment or function call up above failed or returned an unexpected result.
You have a variable that is equal to None and you're attempting to access an attribute of it called 'something'.
Copyfoo = None
foo.something = 1
or
Copyfoo = None
print(foo.something)
Both will yield an AttributeError: 'NoneType'