for loop python error - python

When i run this function i get the error, int object is not iterable. Here is the function
def WriteData(header, column):
if isinstance(header, str):
worksheet.write_string('A1', header)
elif isinstance(header, list):
worksheet.write_row('A1', header)
else:
worksheet.write_string('A1', 'no header data')
if isinstance(column, str):
worksheet.write_column('A2', column)
elif isinstance(column, list):
for col in range(0,len(column)):
worksheet.write_column('A2',col)
Here how i call the function:
WriteData(header=['Freq', 'Volts', 'VoltsPhase'], column=[frequencies, volts, voltsPhase])
The error is in the for loop for the specific call that i do this is what i am expecting:
for col in range(0,3)
This is correct as far as i know. So where is the error?
Traceback (most recent call last):
File "C:\Src32\PythonScripts\3300_Utilities\Run3300TestProcedure.py", line 415, in <module>
WriteData(header=['Freq', 'Volts', 'VoltsPhase'], column=[frequencies, volts, voltsPhase])
File "C:\Src32\PythonScripts\3300_Utilities\Run3300TestProcedure.py", line 413, in WriteData
worksheet.write_column('A2',col)
File "C:\Program Files (x86)\WinPython-32bit-3.4.4.4Qt5\python-3.4.4\lib\site-packages\xlsxwriter\worksheet.py", line 64, in cell_wrapper
return method(self, *args, **kwargs)
File "C:\Program Files (x86)\WinPython-32bit-3.4.4.4Qt5\python-3.4.4\lib\site-packages\xlsxwriter\worksheet.py", line 1012, in write_column
for token in data:
TypeError: 'int' object is not iterable

This is the correct way to do it:
for col in range(0,len(column)):
worksheet.write_column(1, col, column[col])

Related

pandas python 3.9 object is not callable

simple code works under python 3.8 but raise error under 3.9.
the pandas series.mean() raise the following:
Traceback (most recent call last):
File "/home/odin/Learn/Udemy/python_100/day_25/main.py", line 8, in <module>
print(data["temp"].mean())
File "/usr/local/lib/python3.9/dist-packages/pandas/core/generic.py", line 11118, in mean
return NDFrame.mean(self, axis, skipna, level, numeric_only, **kwargs)
File "/usr/local/lib/python3.9/dist-packages/pandas/core/generic.py", line 10726, in mean
return self._stat_function(
File "/usr/local/lib/python3.9/dist-packages/pandas/core/generic.py", line 10711, in _stat_function
return self._reduce(
File "/usr/local/lib/python3.9/dist-packages/pandas/core/series.py", line 4182, in _reduce
return op(delegate, skipna=skipna, **kwds)
File "/usr/local/lib/python3.9/dist-packages/pandas/core/nanops.py", line 71, in _f
return f(*args, **kwargs)
File "/usr/local/lib/python3.9/dist-packages/pandas/core/nanops.py", line 124, in f
result = bn_func(values, axis=axis, **kwds)
TypeError: 'NoneType' object is not callable
the sample code:
import pandas as pd
data = pd.read_csv("weather_data_n.csv")
with pd.option_context('display.max_rows', None, 'display.max_columns', None):
print(data)
temp_list = data["temp"].to_list()
print(data["temp"].mean())

from_personal_row() takes 1 positional argument but 2 were given

The code below (and linked in full here) is attempting to read from a .csv uploaded to Google Sheets, however I cannot get past the following error:
Traceback (most recent call last):
File "import_report.py", line 211, in <module>
main()
File "import_report.py", line 163, in main
all_trackings.extend(objects_to_sheet.download_from_sheet(from_personal_row, sheet_id, tab_name))
File "C:\Users\xxx\AppData\Local\Programs\Python\Python38\lib\site-packages\tenacity\__init__.py", line 329, in wrapped_f
return self.call(f, *args, **kw)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python38\lib\site-packages\tenacity\__init__.py", line 409, in call
do = self.iter(retry_state=retry_state)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python38\lib\site-packages\tenacity\__init__.py", line 368, in iter
raise retry_exc.reraise()
File "C:\Users\xxx\AppData\Local\Programs\Python\Python38\lib\site-packages\tenacity\__init__.py", line 186, in reraise
raise self.last_attempt.result()
File "C:\Users\xxx\AppData\Local\Programs\Python\Python38\lib\concurrent\futures\_base.py", line 432, in result
return self.__get_result()
File "C:\Users\xxx\AppData\Local\Programs\Python\Python38\lib\concurrent\futures\_base.py", line 388, in __get_result
raise self._exception
File "C:\Users\xxx\AppData\Local\Programs\Python\Python38\lib\site-packages\tenacity\__init__.py", line 412, in call
result = fn(*args, **kwargs)
File "C:\Users\xxx\Documents\GitHub\order-tracking\lib\objects_to_sheet.py", line 26, in download_from_sheet
return [from_row_fn(header, value) for value in values]
File "C:\Users\xxx\Documents\GitHub\order-tracking\lib\objects_to_sheet.py", line 26, in <listcomp>
return [from_row_fn(header, value) for value in values]
TypeError: from_personal_row() takes 1 positional argument but 2 were given
I've read a lot of threads regarding similar errors other posters have encountered, but I can't figure out how to apply the advice here.
Google Sheet CSV:
Code:
def from_personal_row(row: Dict[str, str]) -> Optional[Tracking]:
tracking_col = row['Carrier Name & Tracking Number']
if not tracking_col:
return None
tracking = tracking_col.split('(')[1].replace(')', '')
orders = {row['Order ID'].upper()}
price_str = str(row['Subtotal']).replace(',', '').replace('$', '').replace('N/A', '0.0')
price = float(price_str) if price_str else 0.0
to_email = row['Ordering Customer Email']
ship_date = get_ship_date(str(row["Shipment Date"]))
street_1 = row['Shipping Address Street 1']
city = row['Shipping Address City']
state = row['Shipping Address State']
address = f"{street_1} {city}, {state}"
group, reconcile = get_group(address)
if group is None:
return None
tracked_cost = 0.0
items = price_str
merchant = 'Amazon'
return Tracking(
tracking,
group,
orders,
price,
to_email,
ship_date=ship_date,
tracked_cost=tracked_cost,
items=items,
merchant=merchant,
reconcile=reconcile)

Pandas AttributeError: 'DataFrame' object has no attribute 'Timestamp'

so i want to get the monthly sum with my script but i always get an AttributeError, which i dont understand. The column Timestamp does indeed exist on my combined_csv. I know for sure that this line is causing the problem since i tested al of my other code before.
AttributeError: 'DataFrame' object has no attribute 'Timestamp'
I'll appreciate every kind of help i can get - thanks
import os
import glob
import pandas as pd
# set working directory
os.chdir("Path to CSVs")
# find all csv files in the folder
# use glob pattern matching -> extension = 'csv'
# save result in list -> all_filenames
extension = 'csv'
all_filenames = [i for i in glob.glob('*.{}'.format(extension))]
# print(all_filenames)
# combine all files in the list
combined_csv = pd.concat([pd.read_csv(f, sep=';') for f in all_filenames])
# Format CSV
# Transform Timestamp column into datetime
combined_csv['Timestamp'] = pd.to_datetime(combined_csv.Timestamp)
# Read out first entry of every day of every month
combined_csv = round(combined_csv.resample('D', on='Timestamp')['HtmDht_Energy'].agg(['first']))
# To get the yield of day i have to subtract day 2 HtmDht_Energy - day 1 HtmDht_Energy
combined_csv["dailyYield"] = combined_csv["first"] - combined_csv["first"].shift()
# combined_csv.reset_index()
# combined_csv.index.set_names(["year", "month"], inplace=True)
combined_csv["monthlySum"] = combined_csv.groupby([combined_csv.Timestamp.dt.year, combined_csv.Timestamp.dt.month]).sum()
Output of combined_csv.columns
Index(['Timestamp', 'teHst0101', 'teHst0102', 'teHst0103', 'teHst0104',
'teHst0105', 'teHst0106', 'teHst0107', 'teHst0201', 'teHst0202',
'teHst0203', 'teHst0204', 'teHst0301', 'teHst0302', 'teHst0303',
'teHst0304', 'teAmb', 'teSolFloHexHst', 'teSolRetHexHst',
'teSolCol0501', 'teSolCol1001', 'teSolCol1501', 'vfSol', 'prSolRetSuc',
'rdGlobalColAngle', 'gSolPump01_roActual', 'gSolPump02_roActual',
'gHstPump03_roActual', 'gHstPump04_roActual', 'gDhtPump06_roActual',
'gMB01_isOpened', 'gMB02_isOpened', 'gCV01_posActual',
'gCV02_posActual', 'HtmDht_Energy', 'HtmDht_Flow', 'HtmDht_Power',
'HtmDht_Volume', 'HtmDht_teFlow', 'HtmDht_teReturn', 'HtmHst_Energy',
'HtmHst_Flow', 'HtmHst_Power', 'HtmHst_Volume', 'HtmHst_teFlow',
'HtmHst_teReturn', 'teSolColDes', 'teHstFloDes'],
dtype='object')
Traceback:
When i select it with
combined_csv["monthlySum"] = combined_csv.groupby([combined_csv['Timestamp'].dt.year, combined_csv['Timestamp'].dt.month]).sum()
Traceback (most recent call last):
File "D:\Users\wink\PycharmProjects\csvToExcel\main.py", line 28, in <module>
combined_csv["monthlySum"] = combined_csv.groupby([combined_csv['Timestamp'].dt.year, combined_csv['Timestamp'].dt.month]).sum()
File "D:\Users\wink\PycharmProjects\csvToExcel\venv\lib\site-packages\pandas\core\frame.py", line 3024, in __getitem__
indexer = self.columns.get_loc(key)
File "D:\Users\wink\PycharmProjects\csvToExcel\venv\lib\site-packages\pandas\core\indexes\base.py", line 3082, in get_loc
raise KeyError(key) from err
KeyError: 'Timestamp'
traceback with mustafas solution
Traceback (most recent call last):
File "C:\Users\winklerm\PycharmProjects\csvToExcel\venv\lib\site-packages\pandas\core\frame.py", line 3862, in reindexer
value = value.reindex(self.index)._values
File "C:\Users\winklerm\PycharmProjects\csvToExcel\venv\lib\site-packages\pandas\util\_decorators.py", line 312, in wrapper
return func(*args, **kwargs)
File "C:\Users\winklerm\PycharmProjects\csvToExcel\venv\lib\site-packages\pandas\core\frame.py", line 4176, in reindex
return super().reindex(**kwargs)
File "C:\Users\winklerm\PycharmProjects\csvToExcel\venv\lib\site-packages\pandas\core\generic.py", line 4811, in reindex
return self._reindex_axes(
File "C:\Users\winklerm\PycharmProjects\csvToExcel\venv\lib\site-packages\pandas\core\frame.py", line 4022, in _reindex_axes
frame = frame._reindex_index(
File "C:\Users\winklerm\PycharmProjects\csvToExcel\venv\lib\site-packages\pandas\core\frame.py", line 4038, in _reindex_index
new_index, indexer = self.index.reindex(
File "C:\Users\winklerm\PycharmProjects\csvToExcel\venv\lib\site-packages\pandas\core\indexes\multi.py", line 2492, in reindex
target = MultiIndex.from_tuples(target)
File "C:\Users\winklerm\PycharmProjects\csvToExcel\venv\lib\site-packages\pandas\core\indexes\multi.py", line 175, in new_meth
return meth(self_or_cls, *args, **kwargs)
File "C:\Users\winklerm\PycharmProjects\csvToExcel\venv\lib\site-packages\pandas\core\indexes\multi.py", line 531, in from_tuples
arrays = list(lib.tuples_to_object_array(tuples).T)
File "pandas\_libs\lib.pyx", line 2527, in pandas._libs.lib.tuples_to_object_array
ValueError: Buffer dtype mismatch, expected 'Python object' but got 'long long'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\winklerm\PycharmProjects\csvToExcel\main.py", line 28, in <module>
combined_csv["monthlySum"] = combined_csv.groupby([combined_csv.Timestamp.dt.year, combined_csv.Timestamp.dt.month]).sum()
File "C:\Users\winklerm\PycharmProjects\csvToExcel\venv\lib\site-packages\pandas\core\frame.py", line 3163, in __setitem__
self._set_item(key, value)
File "C:\Users\winklerm\PycharmProjects\csvToExcel\venv\lib\site-packages\pandas\core\frame.py", line 3242, in _set_item
value = self._sanitize_column(key, value)
File "C:\Users\winklerm\PycharmProjects\csvToExcel\venv\lib\site-packages\pandas\core\frame.py", line 3888, in _sanitize_column
value = reindexer(value).T
File "C:\Users\winklerm\PycharmProjects\csvToExcel\venv\lib\site-packages\pandas\core\frame.py", line 3870, in reindexer
raise TypeError(
TypeError: incompatible index of inserted column with frame index
This line makes the Timestamp column the index of the combined_csv:
combined_csv = round(combined_csv.resample('D', on='Timestamp')['HtmDht_Energy'].agg(['first']))
and therefore you get an error when you try to access .Timestamp.
Remedy is to reset_index, so instead of above line, you can try this:
combined_csv = round(combined_csv.resample('D', on='Timestamp')['HtmDht_Energy'].agg(['first'])).reset_index()
which will take the Timestamp column back into normal columns from the index and you can then access it.
Side note:
combined_csv["dailyYield"] = combined_csv["first"] - combined_csv["first"].shift()
is equivalent to
combined_csv["dailyYield"] = combined_csv["first"].diff()

How do I return the difference between the current date and each query result date?

I want to get the difference between the current date_time and the date_time of each query record. I've tried:
matches = session.query((datetime.now() - Match.date_time_inferred).label("time_diff"))
But I get:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\Philip\Miniconda3\envs\capra\lib\site-packages\sqlalchemy\orm\query.py", line 3149, in __getitem__
def __getitem__(self, item):
File "C:\Users\Philip\Miniconda3\envs\capra\lib\site-packages\sqlalchemy\orm\query.py", line 3149, in __getitem__
def __getitem__(self, item):
File "C:\Users\Philip\Miniconda3\envs\capra\lib\site-packages\sqlalchemy\orm\loading.py", line 35, in instances
def instances(query, cursor, context):
File "C:\Users\Philip\Miniconda3\envs\capra\lib\site-packages\sqlalchemy\util\langhelpers.py", line 62, in __exit__
def __exit__(self, type_, value, traceback):
File "C:\Users\Philip\Miniconda3\envs\capra\lib\site-packages\sqlalchemy\util\compat.py", line 148, in raise_
def raise_(
File "C:\Users\Philip\Miniconda3\envs\capra\lib\site-packages\sqlalchemy\orm\loading.py", line 35, in instances
def instances(query, cursor, context):
File "C:\Users\Philip\Miniconda3\envs\capra\lib\site-packages\sqlalchemy\orm\loading.py", line 84, in <listcomp>
keyed_tuple([proc(row) for proc in process])
File "C:\Users\Philip\Miniconda3\envs\capra\lib\site-packages\sqlalchemy\orm\loading.py", line 84, in <listcomp>
keyed_tuple([proc(row) for proc in process])
File "C:\Users\Philip\Miniconda3\envs\capra\lib\site-packages\sqlalchemy\sql\sqltypes.py", line 1952, in process
def process(value):
TypeError: unsupported operand type(s) for -: 'float' and 'datetime.datetime'
Where is the float coming from? Table1.date_time is definitely a date_time field...
You can also use SQL functions for that. For example, for PostgreSQL, it would be
matches = session.query((func.now() - Match.date_time_inferred).label("time_diff"))
Then it only gets processed when it's at the database level, but you shouldn't get problems with values being the wrong type.

Pandas Reduction Error when trying to update dataframe

I need to update some of the data in my dataframe in the same sense of a update query in SQL. My current code is as follows:
import pandas
df = pandas.read_csv('filee.csv') # load trades from csv file
def updateDataframe(row):
if row['Name'] == "Joe":
return "Black"
else:
return row
df['LastName'] = df.apply(updateDataframe,axis=1)
However, it returns the following error:
Traceback (most recent call last):
File "test.py", line 11, in <module>
df['LastName'] = df.apply(updateDataframe,axis=1)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/frame.py", line 2038, in __setitem__
self._set_item(key, value)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/frame.py", line 2085, in _set_item
NDFrame._set_item(self, key, value)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/generic.py", line 582, in _set_item
self._data.set(key, value)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/internals.py", line 1459, in set
_set_item(self.items[loc], value)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/internals.py", line 1454, in _set_item
block.set(item, arr)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/internals.py", line 176, in set
self.values[loc] = value
ValueError: output operand requires a reduction, but reduction is not enabled
How do I resolve this. Or is there a better way to accomplish what I am trying to do?
#Jeff has a good concise implementation of your problem in the comments above, but if you want to fix the error in your code, try the following:
For the file filee.csv with the following contents:
Name,LastName
Andy,Blue
Joe,Smith
After the else, you need to return a Last Name string rather than a row object, as shown below:
import pandas
df = pandas.read_csv('filee.csv') # load trades from csv file
def updateDataframe(row):
if row['Name'] == "Joe":
return "Black"
else:
return row['LastName']
df['LastName'] = df.apply(updateDataframe,axis=1)
print df
results in the the following output:
Name LastName
0 Andy Blue
1 Joe Black

Categories

Resources