Read all range names and values - python

Follow up to Felix's response:
I am still getting errors after changing refers_to_range.value to name.
Below is the updated code and the error output from Jupyter.
If I use a single range name and then lookup the value, it works fine, but when I try to go through the list of values returned by the "for name in wb.names", I am not able to return a value for that list.
import xlwings as xw
wb = xw.Book(r'C:\Agency\wkbk utility\Uploaded to HDS\108 Place.xlsm')
for name in wb.names:
range_value = wb.names(name).name
print(range_value)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-e5c7c7a0215d> in <module>
2
3 for name in wb.names:
----> 4 range_value = wb.names(name).name
5
6 print(range_value)
~\Anaconda3\lib\site-packages\xlwings\main.py in __call__(self, name_or_index)
2519
2520 def __call__(self, name_or_index):
-> 2521 return Name(impl=self.impl(name_or_index))
2522
2523 def contains(self, name_or_index):
~\Anaconda3\lib\site-packages\xlwings\_xlwindows.py in __call__(self, name_or_index)
1348
1349 def __call__(self, name_or_index):
-> 1350 return Name(xl=self.xl(name_or_index))
1351
1352 def contains(self, name_or_index):
~\Anaconda3\lib\site-packages\xlwings\_xlwindows.py in __call__(self, *args, **kwargs)
150 for i in range(N_COM_ATTEMPTS + 1):
151 try:
--> 152 v = self._inner(*args, **kwargs)
153 t = type(v)
154 if t is CDispatch:
~\Anaconda3\lib\site-packages\win32com\client\dynamic.py in __call__(self, *args)
195 if invkind is not None:
196 allArgs = (dispid,LCID,invkind,1) + args
--> 197 return self._get_good_object_(self._oleobj_.Invoke(*allArgs),self._olerepr_.defaultDispatchName,None)
198 raise TypeError("This dispatch object does not define a default method")
199
TypeError: Objects of type 'Name' can not be converted to a COM VARIANT (but obtaining the buffer() of this object could)

Related

Why does the Ray returns the FUNCTION_SIZE_ERROR_THRESHOLD?

I just started to use Ray for handling the data.
I tried to install the code below,
import ray
ray.init(num_cpus = 16, ignore_reinit_error=True, object_store_memory = 10**10)
#ray.remote
def PROB_SCORES(words):
encoded_tweet = tokenizer(words, return_tensors = 'pt')
output = model(**encoded_tweet)
# convert the tensor in probability
scores = output[0][0].detach().numpy()
scores = softmax(scores)
prob_scores = scores[0] * (-1) + scores[-1] * 1 # neg to -1, pos to 1
return prob_scores
ray.get([PROB_SCORES.remote(Tweets['Content'])]) # 'Tweets' is the dataframe which contains data.
However, I get the error message like this,
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
c:\Users\HERO\OneDrive\Github\[i-MAES]\[TextAnalysis]\TextAnalysis\NLTK_STUDY\2. Senti.ipynb Cell 11' in <cell line: 1>()
----> 1 ray.get([PROB_SCORES.remote(S8['Content'])])
File c:\Users\HERO\anaconda3\envs\text\lib\site-packages\ray\remote_function.py:111, in RemoteFunction.__init__.<locals>._remote_proxy(*args, **kwargs)
109 #wraps(function)
110 def _remote_proxy(*args, **kwargs):
--> 111 return self._remote(args=args, kwargs=kwargs, **self._default_options)
File c:\Users\HERO\anaconda3\envs\text\lib\site-packages\ray\util\tracing\tracing_helper.py:303, in _tracing_task_invocation.<locals>._invocation_remote_span(self, args, kwargs, *_args, **_kwargs)
301 if kwargs is not None:
302 assert "_ray_trace_ctx" not in kwargs
--> 303 return method(self, args, kwargs, *_args, **_kwargs)
305 assert "_ray_trace_ctx" not in kwargs
307 tracer = _opentelemetry.trace.get_tracer(__name__)
File c:\Users\HERO\anaconda3\envs\text\lib\site-packages\ray\remote_function.py:213, in RemoteFunction._remote(self, args, kwargs, **task_options)
210 raise TypeError(msg) from e
212 self._last_export_session_and_job = worker.current_session_and_job
--> 213 worker.function_actor_manager.export(self)
215 kwargs = {} if kwargs is None else kwargs
216 args = [] if args is None else args
File c:\Users\HERO\anaconda3\envs\text\lib\site-packages\ray\_private\function_manager.py:205, in FunctionActorManager.export(self, remote_function)
...
653 ray_constants.FUNCTION_SIZE_ERROR_THRESHOLD // (1024 * 1024),
654 )
--> 655 raise ValueError(error)
ValueError: The remote function __main__.PROB_SCORES is too large (476 MiB > FUNCTION_SIZE_ERROR_THRESHOLD=95 MiB). Check that its definition is not implicitly capturing a large array or other object in scope. Tip: use ray.put() to put large objects in the Ray object store.
I have no idea why this problem is happening. The 'Tweets' dataframe consumes the memory of only about 5MB.
Any help would be my lifesaver.

Overriding * imports globally for jupyter

I'm running jupyter lab on windows and fastai.vision.utils.verify_images(fns) is giving me problems because it calls fastcore.parallel.parallel with default n_workers=8. There are many ways around it, but I was trying to figure out a code block that I could slap in any notebook and have it so all underlying calls to parallel will run with n_workers=1.
I tried the following cell:
import fastcore
import sys
_fastcore = fastcore
_parallel = lambda *args, **kwargs: fastcore.parallel.parallel(*args, **kwargs, n_workers=1)
_fastcore.parallel.parallel = _parallel
sys.modules['fastcore'] = _fastcore
fastcore.parallel.parallel
printing
<function __main__.<lambda>(*args, **kwargs)>
but when I try running verify_images it still fails as if the patch never happened
---------------------------------------------------------------------------
BrokenProcessPool Traceback (most recent call last)
<ipython-input-37-f1773f2c9e62> in <module>
3 # from mock import patch
4 # with patch('fastcore.parallel.parallel') as _parallel:
----> 5 failed = verify_images(fns)
6 # failed = L(fns[i] for i,o in enumerate(_parallel(verify_image, fns)) if not o)
7 failed
~\anaconda3\lib\site-packages\fastai\vision\utils.py in verify_images(fns)
59 def verify_images(fns):
60 "Find images in `fns` that can't be opened"
---> 61 return L(fns[i] for i,o in enumerate(parallel(verify_image, fns)) if not o)
62
63 # Cell
~\anaconda3\lib\site-packages\fastcore\parallel.py in parallel(f, items, n_workers, total, progress, pause, threadpool, timeout, chunksize, *args, **kwargs)
121 if total is None: total = len(items)
122 r = progress_bar(r, total=total, leave=False)
--> 123 return L(r)
124
125 # Cell
~\anaconda3\lib\site-packages\fastcore\foundation.py in __call__(cls, x, *args, **kwargs)
95 def __call__(cls, x=None, *args, **kwargs):
96 if not args and not kwargs and x is not None and isinstance(x,cls): return x
---> 97 return super().__call__(x, *args, **kwargs)
98
99 # Cell
~\anaconda3\lib\site-packages\fastcore\foundation.py in __init__(self, items, use_list, match, *rest)
103 def __init__(self, items=None, *rest, use_list=False, match=None):
104 if (use_list is not None) or not is_array(items):
--> 105 items = listify(items, *rest, use_list=use_list, match=match)
106 super().__init__(items)
107
~\anaconda3\lib\site-packages\fastcore\basics.py in listify(o, use_list, match, *rest)
54 elif isinstance(o, list): res = o
55 elif isinstance(o, str) or is_array(o): res = [o]
---> 56 elif is_iter(o): res = list(o)
57 else: res = [o]
58 if match is not None:
~\anaconda3\lib\concurrent\futures\process.py in _chain_from_iterable_of_lists(iterable)
482 careful not to keep references to yielded objects.
483 """
--> 484 for element in iterable:
485 element.reverse()
486 while element:
~\anaconda3\lib\concurrent\futures\_base.py in result_iterator()
609 # Careful not to keep a reference to the popped future
610 if timeout is None:
--> 611 yield fs.pop().result()
612 else:
613 yield fs.pop().result(end_time - time.monotonic())
~\anaconda3\lib\concurrent\futures\_base.py in result(self, timeout)
437 raise CancelledError()
438 elif self._state == FINISHED:
--> 439 return self.__get_result()
440 else:
441 raise TimeoutError()
~\anaconda3\lib\concurrent\futures\_base.py in __get_result(self)
386 def __get_result(self):
387 if self._exception:
--> 388 raise self._exception
389 else:
390 return self._result
BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending.
I suspect it has to do with fastai.vision.utils using * imports for fastcore. Is there a way to achieve what I want?
Since the parallel function has already been imported into the fastai.vision.utils module, the correct way is to monkeypatch that module rather than fastcore.parallel:
... # your code for custom `parallel` function goes here
import fastai.vision.utils
fastai.vision.utils.parallel = _parallel # assign your custom function here

Attribute error applying a transform across a list of columns in a pandas dataframe

Hi I have a dataframe and a list of columns I want to perform a loop over:
#list of the 4 columns i want to perform function on
columnnames= ['a','b','c','d']
#Function
for col in columnnames:
df[f"{col}_new"] = df.groupby('Name')[col].transform(lambda x: x.rolling(20).apply(ewma).shift())
When I run this loop I get the following error:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-21-519963825599> in <module>
8 ewma = partial(np.average, weights=weights)
9 for col in columnnames:
---> 10 df[f"{col}_new"] = df.groupby('Name')[col].transform(lambda x: x.rolling(20).apply(ewma).shift())
11
12 #df['testcolumn'] = df['test']*2
~\anaconda3\lib\site-packages\pandas\core\groupby\generic.py in transform(self, func, *args, **kwargs)
463
464 if not isinstance(func, str):
--> 465 return self._transform_general(func, *args, **kwargs)
466
467 elif func not in base.transform_kernel_whitelist:
~\anaconda3\lib\site-packages\pandas\core\groupby\generic.py in _transform_general(self, func, *args, **kwargs)
507 # we have a numeric dtype, as these are *always* user-defined funcs
508 # the cython take a different path (and casting)
--> 509 dtype = self._selected_obj.dtype
510 if is_numeric_dtype(dtype):
511 result = maybe_downcast_to_dtype(result, dtype)
~\anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
5272 if self._info_axis._can_hold_identifiers_and_holds_name(name):
5273 return self[name]
-> 5274 return object.__getattribute__(self, name)
5275
5276 def __setattr__(self, name: str, value) -> None:
AttributeError: 'DataFrame' object has no attribute 'dtype'
Any ideas on what my issue could be?
I have run this successfully on a different dataframe (which is very similar) so i just dont know what this error is telling me.
Thanks very much!
Turns out my error was that I had some columns with the same name!

TypeError: unhashable type: 'numpy.ndarray' with Pandas after importing data from MYSQL

I ran the same program on two different data sources (a CSV file & MYSQL database), CSV import runs fine but MYSQL import throws the numpy type error:
I'm guessing issue may lie with these 2 points:
1. Data import issues - INT, TEXT etc? I'm using VARCHAR for the data.
2. Issue with how matplotlib works with Panda dataframes?
I'm new so please treat me as one :)
import pandas as pd
import numpy as np
import matplotlib.pyplot as pp
import seaborn
from sqlalchemy import create_engine
import pymysql
engine = create_engine("mysql+pymysql://root:root#127.0.0.1:3306/babynames",echo=False)
names = pd.read_sql_query('select * from BABYNAMES',engine)
names_indexed = names.set_index(['gender','name','year']).sort_index()
def plotname(gender, name):
data = names_indexed.loc[gender, name]
pp.plot(data.index, data.values)
plotname('F','Nancy')
Error code:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-32-9d981bcf8365> in <module>()
----> 1 plotname('F','Nancy')
<ipython-input-31-85c728659ad0> in plotname(gender, name)
1 def plotname(gender, name):
2 data = allyears_indexed.loc[gender, name]
----> 3 pp.plot(data.index, data.values)
~/anaconda3/lib/python3.7/site-packages/matplotlib/pyplot.py in plot(*args, **kwargs)
3361 mplDeprecation)
3362 try:
-> 3363 ret = ax.plot(*args, **kwargs)
3364 finally:
3365 ax._hold = washold
~/anaconda3/lib/python3.7/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)
1865 "the Matplotlib list!)" % (label_namer, func.__name__),
1866 RuntimeWarning, stacklevel=2)
-> 1867 return func(ax, *args, **kwargs)
1868
1869 inner.__doc__ = _add_data_doc(inner.__doc__,
~/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_axes.py in plot(self, *args, **kwargs)
1526 kwargs = cbook.normalize_kwargs(kwargs, _alias_map)
1527
-> 1528 for line in self._get_lines(*args, **kwargs):
1529 self.add_line(line)
1530 lines.append(line)
~/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_base.py in _grab_next_args(self, *args, **kwargs)
404 this += args[0],
405 args = args[1:]
--> 406 for seg in self._plot_args(this, kwargs):
407 yield seg
408
~/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_base.py in _plot_args(self, tup, kwargs)
381 x, y = index_of(tup[-1])
382
--> 383 x, y = self._xy_from_xy(x, y)
384
385 if self.command == 'plot':
~/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_base.py in _xy_from_xy(self, x, y)
214 if self.axes.xaxis is not None and self.axes.yaxis is not None:
215 bx = self.axes.xaxis.update_units(x)
--> 216 by = self.axes.yaxis.update_units(y)
217
218 if self.command != 'plot':
~/anaconda3/lib/python3.7/site-packages/matplotlib/axis.py in update_units(self, data)
1467 neednew = self.converter != converter
1468 self.converter = converter
-> 1469 default = self.converter.default_units(data, self)
1470 if default is not None and self.units is None:
1471 self.set_units(default)
~/anaconda3/lib/python3.7/site-packages/matplotlib/category.py in default_units(data, axis)
113 # default_units->axis_info->convert
114 if axis.units is None:
--> 115 axis.set_units(UnitData(data))
116 else:
117 axis.units.update(data)
~/anaconda3/lib/python3.7/site-packages/matplotlib/category.py in __init__(self, data)
180 self._counter = itertools.count(start=0)
181 if data is not None:
--> 182 self.update(data)
183
184 def update(self, data):
~/anaconda3/lib/python3.7/site-packages/matplotlib/category.py in update(self, data)
197 data = np.atleast_1d(np.array(data, dtype=object))
198
--> 199 for val in OrderedDict.fromkeys(data):
200 if not isinstance(val, VALID_TYPES):
201 raise TypeError("{val!r} is not a string".format(val=val))
TypeError: unhashable type: 'numpy.ndarray'
I was right, the issue was with the data types. I created a new table replacing VARCHAR for INT and then used the same code from above and the program worked perfectly.

Cannot Load From HDF Data Store

I am using pandas 0.11.0 on Windows 64 Bit.
I have a datastore:
store = pandas.io.pytables.HDFStore( r"""C:\data.h5""" )
It has some data in it:
print(store)
<class 'pandas.io.pytables.HDFStore'>
File path: C:\data.h5
/region frame (shape->[30,2])
/sector frame (shape->[116,2])
When I try to load 'sector' from the store I get:
store['/sector']
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-24-7fea069742ed> in <module>()
----> 1 store['/sector']
C:\Anaconda\lib\site-packages\pandas\io\pytables.pyc in __getitem__(self, key)
236
237 def __getitem__(self, key):
--> 238 return self.get(key)
239
240 def __setitem__(self, key, value):
C:\Anaconda\lib\site-packages\pandas\io\pytables.pyc in get(self, key)
369 if group is None:
370 raise KeyError('No object named %s in the file' % key)
--> 371 return self._read_group(group)
372
373 def select(self, key, where=None, start=None, stop=None, columns=None, iterator=False, chunksize=None, **kwargs):
C:\Anaconda\lib\site-packages\pandas\io\pytables.pyc in _read_group(self, group, **kwargs)
876 s = self._create_storer(group)
877 s.infer_axes()
--> 878 return s.read(**kwargs)
879
880 class TableIterator(object):
C:\Anaconda\lib\site-packages\pandas\io\pytables.pyc in read(self, **kwargs)
1982 blocks = []
1983 for i in range(self.nblocks):
-> 1984 blk_items = self.read_index('block%d_items' % i)
1985 values = self.read_array('block%d_values' % i)
1986 blk = make_block(values, blk_items, items)
C:\Anaconda\lib\site-packages\pandas\io\pytables.pyc in read_index(self, key)
1615 return self.read_sparse_intindex(key)
1616 elif variety == 'regular':
-> 1617 _, index = self.read_index_node(getattr(self.group, key))
1618 return index
1619 else: # pragma: no cover
C:\Anaconda\lib\site-packages\pandas\io\pytables.pyc in read_index_node(self, node)
1733 **kwargs)
1734 else:
-> 1735 index = factory(_unconvert_index(data, kind), **kwargs)
1736
1737 index.name = name
TypeError: __new__() got an unexpected keyword argument 'freq'
What is this "freq" argument? And why is it stopping me from loading the data?

Categories