I have a list of part numbers that I want to use to extract a list of prices on a website.
However I'm getting the below error when running the code:
Traceback (most recent call last):
File "C:/Users/212677036/.PyCharmCE2019.1/config/scratches/scratch_1.py", line 13, in
data = {"partOptionFilter": {"PartNumber": PN(i), "AlternativeOemId": "17155"}}
TypeError: 'DataFrame' object is not callable
Process finished with exit code 1
import requests
import pandas as pd
df = pd.read_excel(r'C:\Users\212677036\Documents\Copy of MIC Parts Review - July 26 19.xlsx')
PN = pd.DataFrame(df, columns = ['Product code'])
#print(PN)
i = 0
Total_rows = PN.shape[0]
while i < Total_rows:
data = {"partOptionFilter": {"PartNumber": PN(i), "AlternativeOemId": "17155"}}
r = requests.post('https://www.partsfinder.com/Catalog/Service/GetPartOptions', json=data).json()
print(r['Data']['PartOptions'][0]['YourPrice'])
i=i+1
You are calling PN(i). That is why it says
TypeError: 'DataFrame' object is not callable
The (i) is like a method call.
I am not sure how your df looks like and what you want to extract but you have to index the DataFrame like this:
PN[i]
or
PN.loc[i, 'columnname']
or
PN.iloc[i, 0]
or ... depending on your df
Related
When I want to remove some elements which satisfy a particular condition, python is throwing up the following error:
TypeError Traceback (most recent call last)
<ipython-input-25-93addf38c9f9> in <module>()
4
5 df = pd.read_csv('fb441e62df2d58994928907a91895ec62c2c42e6cd075c2700843b89.csv;
----> 6 df = filter(df,~('-02-29' in df['Date']))
7 '''tmax = []; tmin = []
8 for dates in df['Date']:
TypeError: 'int' object is not iterable
The following is the code :
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('data/C2A2_data/BinnedCsvs_d400/fb441e62df2d58994928907a91895ec62c2c42e6cd075c2700843b89.csv');
df = filter(df,~('-02-29' in df['Date']))
What wrong could I be doing?
Following is sample data
Sample Data
Use df.filter() (https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.filter.html)
Also please attach the csv so we can run it locally.
Another way to do this is to use one of pandas' string methods for Boolean indexing:
df = df[~ df['Date'].str.contains('-02-29')]
You will still have to make sure that all the dates are actually strings first.
Edit:
Seeing the picture of your data, maybe this is what you want (slashes instead of hyphens):
df = df[~ df['Date'].str.contains('/02/29')]
I am trying to export the summary of my multiple regression models in a table.
results = {'A':result.summary(),
'B': result1.summary(), 'C': result2.summary(), 'D': result3.summary(), 'E' : result4.summary()}
df2 = pd.DataFrame({'Model':[], 'Param':[], 'Value':[]})
for mod in results.keys():
for col in results[mod].tables[0].columns:
if col % 2 == 0:
df2 = df2.append(pd.DataFrame({'Model': [mod]*results[mod].tables[0][col].size,
'Param':results[mod].tables[0][col].values,
'Value':results[mod].tables[0][col+1].values}))
print(df2)
When I run the code it gives me error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-280-952fff354224> in <module>
3 df2 = pd.DataFrame({'Model':[], 'Param':[], 'Value':[]})
4 for mod in results.keys():
----> 5 for col in results[mod].tables[0].column:
6 if col % 2 == 0:
7 df2 = df2.append(pd.DataFrame({'Model': [mod]*results[mod].tables[0][col].size,
AttributeError: 'SimpleTable' object has no attribute 'column'
The SimpleTable in this context is statsmodels.iolib.table.SimpleTable. We can use pandas.DataFrame.from_records to convert the data type to DataFrame. From here, you can access the columns easily.
Assure this SimpleTable is accessed through a variable named "t"
df = pd.DataFrame.from_records(t.data)
header = df.iloc[0] # grab the first row for the header
df = df[1:] # take the data less the header row
df.columns = header
print(df.shape)
return df['your_col_name']
It's hard to tell without seeing how you're creating result.summary() et al, but it's likely that the SimpleTable API follows similar/related pandas APIs, in which case you're looking for the columns attribute (note the plural 's').
I am trying to adapt a Pandas.Series with a certain frequency to a Pandas.Series with a different frequency. Therefore I used the resample function but it does not recognize for instance that 'M' is a subperiod of '3M' and raised an error
import pandas as pd
idx_1 = pd.period_range('2017-01-01', periods=6, freq='M')
data_1 = pd.Series(range(6), index=idx_1)
data_higher_freq = data_1.resample('3M', kind="Period").sum()
Raises the following exception:
Traceback (most recent call last): File "/home/mitch/Programs/Infrastructure_software/Sandbox/spyderTest.py", line 15, in <module>
data_higher_freq = data_1.resample('3M', kind="Period").sum() File "/home/mitch/anaconda3/lib/python3.6/site-packages/pandas/core/resample.py", line 758, in f return self._downsample(_method, min_count=min_count) File "/home/mitch/anaconda3/lib/python3.6/site-packages/pandas/core/resamplepy", line 1061, in _downsample 'sub or super periods'.format(ax.freq, self.freq))
pandas._libs.tslibs.period.IncompatibleFrequency: Frequency <MonthEnd> cannot be resampled to <3 * MonthEnds>, as they are not sub or super periods
This seems to be due to the pd.tseries.frequencies.is_subperiod function:
import pandas as pd
pd.tseries.frequencies.is_subperiod('M', '3M')
pd.tseries.frequencies.is_subperiod('M', 'Q')
Indeed it returns False for the first command and True for the second.
I would really appreciate any hints about any solution.
Thks.
Try changing from PeriodIndex to DateTimeIndex before resampling:
import pandas as pd
idx_1 = pd.period_range('2017-01-01', periods=6, freq='M')
data_1 = pd.Series(range(6), index=idx_1)
data_1.index = data_1.index.astype('datetime64[ns]')
data_higher_freq = data_1.resample('3M', kind='period').sum()
Output:
data_higher_freq
Out[582]:
2017-01 3
2017-04 12
Freq: 3M, dtype: int64
The code below starts by uploading data from yahoo finance in pnls. It does this successufly. Then I have code where I want to add columns to the loaded data in pnls. This part is unsuccessful. Finally I want to save pnls to a csv file which it does successfully if the column changes are not in the code.
The question that I need answered is how do I successfully make multiple column changes before I save pnls to csv??
from pandas_datareader import data as dreader
import pandas as pd
from datetime import datetime
import numpy as np
# Symbols is a list of all the ticker symbols that I am downloading from yahoo finance
symbols = ['tvix','pall','pplt','sivr','jju','nib','jo','jjc','bal','jjg','ld','cdw','gaz','jjn','pgm',
'sgg','jjt','grn','oil','gld','corn','soyb','weat','uhn','uga','bunl','jgbl','bndx','aunz',
'cemb','emhy','vwob','ald','udn','fxa','fxb']
#This gets the data from yahoo finance
pnls = {i:dreader.DataReader(i,'yahoo','1985-01-01',datetime.today()) for i in symbols}
# These are the new columns that I want to add
pnls['PofChg'] = ((pnls.Close - pnls.Open) / (pnls.Open)) * 100
pnls['U_D_F'] = np.where(pnls.PofChg > 0 , 'Up', np.where(pnls.PofChg == 0, 'Flat', 'Down'));pnls
pnls['Up_Down'] = pnls.Close.diff()
pnls['Close%pd'] = pnls.Close.pct_change()*100
# This saves the current ticker to a csv file
for df_name in pnls:
pnls.get(df_name).to_csv("{}_data.csv".format(df_name), index=True, header=True)
This is the error that I get when I run the code
Traceback (most recent call last):
File "YahooFinanceDataGetter.py", line 14, in <module>
pnls['PofChg'] = ((pnls.Close - pnls.Open) / (pnls.Open)) * 100
AttributeError: 'dict' object has no attribute 'Close'
Press any key to continue . . .
The line
pnls = {i:dreader.DataReader(i,'yahoo','1985-01-01',datetime.today()) for i in symbols}
builds a python dict (using dictionary comprehension). To check this, you can run the following:
assert type(pnls) == dict
This is what the error message is telling you: AttributeError: 'dict' object has no attribute 'Close'.
The DataFrames are actually the values of the dictionary. To apply transformations to them, you can iterate over the values() of the dictionary:
for df in pnls.values():
df = ((df.Close - df.Open) / (df.Open)) * 100
...
I'm importing a CSV into a pandas data frame, and then I'm trying to create three new columns in that data frame from data retrieved from geopy.geocoders.GoogleV3() :
import pandas from geopy.geocoders import GoogleV3
DATA = pandas.read_csv("file/to/csv")
geolocator = GoogleV3()
DATA.googleaddress, (DATA.latitude, DATA.longitude) = geolocator.geocode(DATA.address)
Problem is I keep getting this error:
Traceback (most recent call last):
File "C:/Path/To/GeoCoder.py", line 9, in <module>
DATA.googleaddress, (DATA.latitude, DATA.longitude) = geolocator.geocode(DATA.address)
TypeError: 'NoneType' object is not iterable
What does this error mean and how do I get around it?
Because geolocator.geocode expects a single argument at a time, not a list (or array).
You could try:
locs = [ geolocator.geocode(addr) for addr in DATA.address ]
geo_info = pandas.DataFrame(
[ (addr.address, addr.latitude, addr.longitude) for addr in locs ],
columns=['googleaddress', 'latitude', 'longitude'])
All you would have to do is merge these DataFrames:
DATA.combine_first(geo_info)
Nnote that it is considered bad form to have an all-uppercase variable in python.