itertools.accumulate() versus functools.reduce()

It seems that accumulate keeps the previous results, whereas reduce (which is known as fold in other languages) does not necessarily. e.g. list(accumulate([1,2,3], operator.add)) would return [1,3,6] whereas a plain fold would return 6 Also (just for fun, don’t do this) you can define accumulate in terms of reduce def accumulate(xs, f): return reduce(lambda a, … Read more

Counting duplicate values in Pandas DataFrame

You can use groupby with function size. Then I reset index with rename column 0 to count. print df Month LSOA code Longitude Latitude Crime type 0 2015-01 E01000916 -0.106453 51.518207 Bicycle theft 1 2015-01 E01000914 -0.111497 51.518226 Burglary 2 2015-01 E01000914 -0.111497 51.518226 Burglary 3 2015-01 E01000914 -0.111497 51.518226 Other theft 4 2015-01 E01000914 … Read more

Client.__init__() “missing 1 required keyword-only argument: ‘intents'”, or “takes 1 positional argument but 2 were given”

You could use the default Intents unless you have a particular one to specify. client = discord.Client(intents=discord.Intents.default()) As the first error message says, it is a keyword-only argument, so you cannot write discord.Client(discord.Intents.default()) without intents=. See Intents for more details.

Decorator that prints function call details (argument names and values)?

Here is the updated version for Python 3.6+ import inspect from functools import wraps def dump_args(func): “”” Decorator to print function call details. This includes parameters names and effective values. “”” @wraps(func) def wrapper(*args, **kwargs): func_args = inspect.signature(func).bind(*args, **kwargs).arguments func_args_str = “, “.join(map(“{0[0]} = {0[1]!r}”.format, func_args.items())) print(f”{func.__module__}.{func.__qualname__} ( {func_args_str} )”) return func(*args, **kwargs) return wrapper … Read more

Get group id back into pandas dataframe

A lot of handy things are stored in the DataFrameGroupBy.grouper object. For example: >>> df = pd.DataFrame({‘Name’: [‘foo’, ‘bar’] * 3, ‘Rank’: np.random.randint(0,3,6), ‘Val’: np.random.rand(6)}) >>> grouped = df.groupby([“Name”, “Rank”]) >>> grouped.grouper. grouped.grouper.agg_series grouped.grouper.indices grouped.grouper.aggregate grouped.grouper.labels grouped.grouper.apply grouped.grouper.levels grouped.grouper.axis grouped.grouper.names grouped.grouper.compressed grouped.grouper.ngroups grouped.grouper.get_group_levels grouped.grouper.nkeys grouped.grouper.get_iterator grouped.grouper.result_index grouped.grouper.group_info grouped.grouper.shape grouped.grouper.group_keys grouped.grouper.size grouped.grouper.groupings grouped.grouper.sort grouped.grouper.groups and so: … Read more

NumPy List Comprehension Syntax

First, you should not be using NumPy arrays as lists of lists. Second, let’s forget about NumPy; your listcomp doesn’t make any sense in the first place, even for lists of lists. In the inner comprehension, for i in X is going to iterate over the rows in X. Those rows aren’t numbers, they’re lists … Read more

Cannot import requests.packages.urllib3.util ‘Retry’

You might need a newer version of Requests. I just tried it with v2.5.1: from requests.packages.urllib3.util import Retry Seems to work. FYI: The latest version is v2.5.3, worth upgrading. Also if you have a reasonably recent version of urllib3 installed separately, this should also work: from urllib3.util import Retry Unfortunately we check the specific isinstance … Read more

How to prevent truncating of string in unit test python

To replace [… chars] and [truncated]… with actual characters (no matter how long, and no matter what the type of the compared values are), add this to your *_test.py file: if ‘unittest.util’ in __import__(‘sys’).modules: # Show full diff in self.assertEqual. __import__(‘sys’).modules[‘unittest.util’]._MAX_LENGTH = 999999999 Indeed, as other answers have noted, setting self.maxDiff = None doesn’t help, … Read more

File not found.