Group a multi-indexed pandas dataframe by one of its levels?

Yes, use the level parameter. Take a look here. Example: In [26]: s first second third bar doo one 0.404705 two 0.577046 baz bee one -1.715002 two -1.039268 foo bop one -0.370647 two -1.157892 qux bop one -1.344312 two 0.844885 dtype: float64 In [27]: s.groupby(level=[‘first’,’second’]).sum() first second bar doo 0.981751 baz bee -2.754270 foo bop … Read more

Replace multiple values with jinja2

Your expression seems to work just fine. If I create a template with the jinja2 expression from your question: >>> import jinja2 >>> t = jinja2.Template(‘{{ test1 | replace(“value1″,”my_value1”) | replace(“value2″,”my_value2”) }}’) And then render it, passing in a value for test1 that contains both of the target replacement strings: >>> output = t.render(test1=”this has … Read more

Hide external modules when importing a module (e.g. regarding code-completion)

I had the same problem, and solved it by adding a leading underscore to all my imports. It doesn’t look brilliant, but it achieves what you’re after. from __future__ import division as _division import numpy as _np import pandas as _pd Only the stuff that starts without an underscore is imported when you import the … Read more

seaborn is not plotting within defined subplots

seaborn.distplot has been DEPRECATED in seaborn 0.11 and is replaced with the following: displot(), a figure-level function with a similar flexibility over the kind of plot to draw. This is a FacetGrid, and does not have the ax parameter, so it will not work with matplotlib.pyplot.subplots. histplot(), an axes-level function for plotting histograms, including with … Read more

Mock variable in function

Just use @patch() to mock out get_complex_data_structure(): @patch(‘module_under_test.get_complex_data_structure’) def test_function_to_test(self, mocked_function): foo_mock = mocked_function.return_value When the test function then calls get_complex_data_structure() a mock object is returned and stored in the local name foo; the very same object that mocked_function.return_value references in the above test; you can use that value to test if do_work() got passed … Read more

Schedule a Python script via batch on windows (using Anaconda)

I would be a bit careful in calling python directly through environment as one never knows if the internals for activate function has changed. I’m just using basic bat-script to help me out. SET log_file=%cd%\logfile.txt call C:\Anaconda3\Scripts\activate.bat cd \script_directory python script.py arg1 arg2 > %log_file% This script saves the log-file wherever the bat is run … Read more

error code: 521