eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I'm selecting text data from an SFrame and printing it. The text is really long, and the cell gets a horizontal scrollbar to view it. I would like to have it wrap to a newline and fit in my window, not to have a horizontal scrollbar. I tried enabling/disabling the vscode command View: Toggle Word Wrap, but that didn't change the output, even upon rerunning the script.

Answer: Unfortunately, VS Code doesn't currently support word wrap in the output or terminal windows. You can try to use textwrap package manually. Add the following codes to your script: import textwrap wrapped_text = textwrap.fill(text, width=80) #text is the object which you want to print print(wrapped_text)
Question: The input has a pattern, every element in the list is a dict and has a fixed keys [{'key': 'a', 'children': [{'key': 'a1', 'children': [{'key': 'a11', 'children': []}]}]}, {'key': 'b', 'children': [{'key': 'b1', 'children': [{'key': 'b11', 'children': []}]}]},]  expected output {'a': {'a1': {'a11': ''}}, 'b': {'b1': {'b11': ''}}}  I want to do it in an iterative way, currently I'm able to get all the value of the fixed key key, but failed to compose to the targetd dict def get_res(stack, key='key'): result = [] while stack: elem = stack.pop() if isinstance(elem, dict): for k, v in elem.items(): if k == key: # breakpoint() result.append(v) stack.append(v) elif isinstance(elem, list): stack.extend(elem) print(result) return result  also stuck in recursive approach def gen_x(stack): for bx in stack: if 'children' not in bx: return {bx['key']: ''} tem_ls = bx['children'] xs = gen_x(tem_ls) print(xs) 

Answer: You can perform a breadth-first traversal with a queue instead: from collections import deque from operator import itemgetter def convert_tree(lst): tree = {} queue = deque([(lst, tree)]) while queue: entries, branch = queue.popleft() for key, children in map(itemgetter('key', 'children'), entries): if children: queue.append((children, branch.setdefault(key, {}))) else: branch[key] = '' return tree  Demo: https://ideone.com/hvfqLj
Question: I'm trying to convert a column of my pandas dataframe from datetime['ns'] to datetime['us']. I've tried using astype but the column type doesn't seem to change. I need the type of the column itself to update due to some downstream libraries checking for a specific column type. >> data[column].dtype.name 'datetime64[ns]' >> data[column] = data[column].astype('datetime64[us]') >> data[column].dtype.name 'datetime64[ns]' 

Answer: I can reproduce, the behaviour you describe, in 1.5.3. So, you're most likely using a similar version or at least older than (2.0.0). Why ? Backwards incompatible API changes :  In past versions, when constructing a Series or DataFrame and passing a datetime64 or timedelta64 dtype with unsupported resolution (i.e. anything other than "ns"), pandas would silently replace the given dtype with its nanosecond analogue.  To get your cast work, you need to update your pandas' version : pip install pandas==2.0.0 # or pip install -U pandas # latest  Minimal Reproducible Example : import pandas as pd # >= 2.0.0 data = pd.DataFrame({"dt": pd.date_range("20240101", freq="T", periods=3)}) data["dt"].dtype.name # 'datetime64[ns]' data["dt"].astype("datetime64[us]").dtype.name # 'datetime64[us]' <<
Question: I have this code that prints the time difference in milliseconds. #!/usr/bin/python import datetime import sys date1= datetime.datetime.strptime('20231107-08:52:53.539', '%Y%m%d-%H:%M:%S.%f') date2= datetime.datetime.strptime('20231107-08:52:53.537', '%Y%m%d-%H:%M:%S.%f') diff = date1-date2 sys.stdout.write(str(diff) + '\t') date1= datetime.datetime.strptime('20231107-08:52:53.537', '%Y%m%d-%H:%M:%S.%f') date2= datetime.datetime.strptime('20231107-08:52:53.537', '%Y%m%d-%H:%M:%S.%f') diff = date1-date2 sys.stdout.write(str(diff) + '\t') date1= datetime.datetime.strptime('20231107-08:52:53.532', '%Y%m%d-%H:%M:%S.%f') date2= datetime.datetime.strptime('20231107-08:52:53.537', '%Y%m%d-%H:%M:%S.%f') diff = date1-date2 sys.stdout.write(str(diff) + '\n')  And it prints this, without consistency  $ python ./prntdatetest.py 0:00:00.002000 <tab> 0:00:00 <tab> -1 day, 23:59:59.995000  I want this to be printed like this  00:00:00.002 <tab> 00:00:00.000 <tab> -0:00:00.005  I do not want to use the print but i want to use stdout.write How can I do this?

Answer: You can define the following formatting function: def format_diff(diff): sign = '-' if diff < datetime.timedelta(0) else '' seconds = abs(diff.total_seconds()) return f'{sign}{seconds//60//60:02.0f}:{seconds//60%60:02.0f}:{seconds%60:06.03f}'  #!/usr/bin/python import datetime import sys date1= datetime.datetime.strptime('20231107-08:52:53.539', '%Y%m%d-%H:%M:%S.%f') date2= datetime.datetime.strptime('20231107-08:52:53.537', '%Y%m%d-%H:%M:%S.%f') diff = date1-date2 sys.stdout.write(format_diff(diff) + '\t') date1= datetime.datetime.strptime('20231107-08:52:53.537', '%Y%m%d-%H:%M:%S.%f') date2= datetime.datetime.strptime('20231107-08:52:53.537', '%Y%m%d-%H:%M:%S.%f') diff = date1-date2 sys.stdout.write(format_diff(diff) + '\t') date1= datetime.datetime.strptime('20231107-08:52:53.532', '%Y%m%d-%H:%M:%S.%f') date2= datetime.datetime.strptime('20231107-08:52:53.537', '%Y%m%d-%H:%M:%S.%f') diff = date1-date2 sys.stdout.write(format_diff(diff) + '\n')  Output: 00:00:00.002 00:00:00.000 -00:00:00.005  Note: that handles values up to on day. For >= 1 day deltas, you need to update the function.
Question: I'm using pd.cut with the keyword argument duplicates='drop'. However, this gives errors when you combine it with the keyword argument labels. The question is similar to this question, but that ignores the label part. Does not work: pd.cut(pd.Series([0, 1, 2, 3, 4, 5]), bins=[0, 1, 1, 2]) Works: pd.cut(pd.Series([0, 1, 2, 3, 4, 5]), bins=[0, 1, 1, 2], duplicates='drop') Does not work: pd.cut(pd.Series([0, 1, 2, 3, 4, 5]), bins=[0, 1, 1, 2], duplicates='drop', labels=[0, 1, 1, 2]) Wouldn't we expect it to drop the label corresponding to the duplicate entry?

Answer: No, the cut documentation is pretty clear, it only concerns the bins:  duplicates {default ‘raise’, ‘drop’}, optional If bin edges are not unique, raise ValueError or drop non-uniques.  Also, in any case the labels must be one value less than the bins, so dropping the labels based on the bins would be ambiguous. This works if you have the correct final number of labels: pd.cut(pd.Series([0, 1, 2, 3, 4, 5]), bins=[0, 1, 1, 2], labels=['a', 'b'], duplicates='drop' )  Or using a weird programmatic alternative: bins = pd.Series([0, 1, 1, 2]) labels = pd.Series(['a', 'b', 'c']) pd.cut(pd.Series([0, 1, 2, 3, 4, 5]), bins=[0, 1, 1, 2], labels=labels[~bins.duplicated()[:-1]], duplicates='drop' )  Output: 0 NaN 1 a 2 b 3 NaN 4 NaN 5 NaN dtype: category Categories (2, object): ['a' < 'b']
Question: I'm trying to follow this tutorial to tune hyperparameters in PyTorch using Ray, copy-pasted everything but I get the following error: ImportError: cannot import name 'Checkpoint' from 'ray.air'  from this line of import: from ray.air import Checkpoint  I installed ray using pip install -U "ray[tune]" as suggested on the official website. After getting the error, to be sure, I also tried a more general pip install ray, which did not fix anything. I have version ray==2.9.0 installed. Any help, please?

Answer: Try to install older version 2.7.0: pip install ray[tune]==2.7.0  Update : For the newest version the Ray AIR session is replaced with a Ray Train context object. You can import Checkpoint using : from ray.train import Checkpoint  You need to adjust your code as follow: from ray import air, train # Ray Train methods and classes: air.session.report -> train.report air.session.get_dataset_shard -> train.get_dataset_shard air.session.get_checkpoint -> train.get_checkpoint air.Checkpoint -> train.Checkpoint air.Result -> train.Result # Ray Train configurations: air.config.CheckpointConfig -> train.CheckpointConfig air.config.FailureConfig -> train.FailureConfig air.config.RunConfig -> train.RunConfig air.config.ScalingConfig -> train.ScalingConfig # Ray TrainContext methods: air.session.get_experiment_name -> train.get_context().get_experiment_name air.session.get_trial_name -> train.get_context().get_trial_name air.session.get_trial_id -> train.get_context().get_trial_id air.session.get_trial_resources -> train.get_context().get_trial_resources air.session.get_trial_dir -> train.get_context().get_trial_dir air.session.get_world_size -> train.get_context().get_world_size air.session.get_world_rank -> train.get_context().get_world_rank air.session.get_local_rank -> train.get_context().get_local_rank air.session.get_local_world_size -> train.get_context().get_local_world_size air.session.get_node_rank -> train.get_context().get_node_rank  For more informations see :  Refining the Ray AIR Surface API 2.7 Migration Guide
Question: Is there an elegant Pythonic way to perform something like rstrip() on a list? Imagine, I have different lists: l1 = ['A', 'D', 'D'] l2 = ['A', 'D'] l3 = ['D', 'A', 'D', 'D'] l4 = ['A', 'D', 'B', 'D']  I need a function that will remove all end-standing 'D' elements from a given list (but not those that come before or in between other elements!). for mylist in [l1, l2, l3, l4]: print(mylist, ' => ', remove_end_elements(mylist, 'D'))  So the desired output would be: ['A', 'D', 'D'] => ['A'] ['A', 'D'] => ['A'] ['D', 'A', 'D', 'D'] => ['D', 'A'] ['A', 'D', 'B', 'D'] => ['A','D','B']  One implementation that does the job is this: def remove_end_elements(mylist, myelement): counter = 0 for element in mylist[::-1]: if element != myelement: break counter -= 1 return mylist[:counter]  Is there a more elegant / efficient way to do it? To answer comment-questions:  Either a new list or modifying the original list is fine (although the above implementation has creating a new list in mind). The real lists contain multi-character-strings (lines from a text file). What I'm actually trying to strip away are lines that fulfill certain criteria for "empty" (no characters OR only whitespace OR only whitespace and commas). I have that check implemented elsewhere. These empty lines can be an arbitrary number at the end of the list, but in most cases will be 1.   I timed the different solutions offered so far, with simulated data close to my actual use case, and the actual is_empty_line() function that I'm using:  Kelly Bundy's solution: 0.029670200019609183 Guy's solution: 0.038380099984351546 my original solution: 0.03837349999230355 cards' solution: 0.0408437000005506 Timeless' solution: 0.08083210000768304  Which one performs better does seem to depend on the complexity of the is_empty_line() function (except for Timeless' solution, which is consistently slower than everything else, and KellyBundy's solution, which is consistently faster).

Answer: def remove_end_elements(mylist, myelement): while myelement in mylist[-1:]: mylist.pop() return mylist
Question: I am refactoring parts of an API and wish to add deprecation warnings to parts that will eventually be removed. However I have stumbled into an issue where I would like to replace a function call with a property sharing a name. Is there a hack where I can support both calling the .length as a property and as a function? I have thinkered with __getattribute__ and __getattr__ and can't think of a way. import warnings class A: @property def length(self): return 1 def length(self): warnings.warn(".length function is deprecated. Use the .length property", DeprecationWarning) return 1  P.S. Preferably I would like the solution to be python 2.7 compatible. Additional context The only "kind of" solution I have thought of is to overwrite the return value and skip the properties for now and add them in later when the deprecation warnings are removed. This solution would work for my case, if there really isn't any other way, but I would prefer a solution that is a lot less hacky. import warnings class F(float): def __init__(self, v): self.v = v def __new__(cls, value): return float.__new__(cls, value) def __call__(self, *args, **kwargs): warnings.warn(".length function is deprecated. Use the .length property", DeprecationWarning) return self.v class A(object): def __getattribute__(self, item): if item == "length": # This is a hack to enable a deprecation warning when calling .length() # Remove this in favor for the @property, when the deprecation warnings are removed. return F(1) return super(A, self).__getattribute__(item) # @property # def length(self): # # type: () -> float # return 1.0 

Answer: One workaround is to make the property return a proxy object of a subtype of the value to be returned. The proxy object can then produce the warning when called: import warnings def warning_property(message, warning_type=DeprecationWarning): class _property(property): def __get__(self, obj, obj_type=None): value = super().__get__(obj, obj_type) class _proxy(type(value)): def __call__(self): warnings.warn(message, warning_type) return value return _proxy(value) return _property  so that: class A: @warning_property(".length function is deprecated. Use the .length property") def length(self): return 1 print(A().length) print(A().length())  outputs: 1 1 DeprecationWarning: .length function is deprecated. Use the .length property  Demo: https://ideone.com/PWkLN9 Note that the above assumes that the constructor of the type of the returning value of the property can take an instance as an argument, which is the case for all built-in types. If the constructor has a different signature then you should modify return _proxy(value) accordingly.
Question: I want to display default values, argument type, and big spacing for --help. But if I do import argparse class F(argparse.MetavarTypeHelpFormatter, argparse.ArgumentDefaultsHelpFormatter, lambda prog: argparse.HelpFormatter(prog, max_help_position = 52)): pass parser = argparse.ArgumentParser( prog = 'junk', formatter_class = F)  It gives the following error TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases  Does anyone know how to combine these three formatters correctly?

Answer: Separating the lambda part works In [122]: class F(argparse.MetavarTypeHelpFormatter, argparse.ArgumentDefaultsHelpFormatter): pass ...: F1 = lambda prog: F(prog, max_help_position=52) ...: parser = argparse.ArgumentParser( ...: prog = 'junk', ...: formatter_class = F1) In [123]: parser.print_help() usage: junk [-h] options: -h, --help show this help message and exit  The lambda expression isn't used to make a new subclass, but to modify how the class is called. In [136]: parser = argparse.ArgumentParser( ...: prog = 'junk', ...: formatter_class = F1) ...: parser.add_argument('--long_name', type=float, default='123.213', help='help line'); In [137]: parser.print_help() usage: junk [-h] [--long_name float] options: -h, --help show this help message and exit --long_name float help line (default: 123.213)  On the argparse bug/issues board it has been suggested that you can subclass the helpformatter by inheriting from several of the provided subclasses. But no one has tested all combinations. The provided subclasses just modify one or more of the formatter methods to produce the desired change. You could make those changes directly in your class. Read the argparse.py code to see how On using the lambda formatter: Explain lambda argparse.HelpFormatter(prog, width) I show how you can do by subclassing, but the lambda expression is simpler. Also where is the documentation for the python argparse helpformatter class?
Question: Consider the following table. All are to be considered boolean except days_old and SKU which are to be considered integer pd.DataFrame({ 'SKU': [10,11,12,13,14,15], 'frozen':[1,0,1,1,1,0], 'vegetable':[1,1,0,1,1,0], 'microwaveable':[1,0,0,0,0,1], 'days_old':[21,9,11,2,6,14], })  I want a co-occurence table of counts that match both the column and index variables and give the average of days_old: Desired output: AVERAGE DAYS OLD: frozen | vegetable | microwaveable frozen 10 | 9.6 | 21 vegetable 9.6 | 9.5 | 21 microw... 21 | 21 | 17.5 

Answer: One option is to use dot: df2 = df[['frozen','vegetable','microwaveable']] df2.T.dot(df2.mul(df['days_old'],axis=0)).div(df2.T.dot(df2)).round(2)  Output:  frozen vegetable microwaveable frozen 10.00 9.67 21.0 vegetable 9.67 9.50 21.0 microwaveable 21.00 21.00 17.5
Question: When im launching my app via CLI, it works without issue  ./org_chart.app/Contents/MacOS/org_chart  however when I launch via double click I met with the error *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: aString != nil'  I used py2app to build the app. Im not sure where to begin debugging this, if someone could point me in the right direction? Thanks for your help! here's the full code for the small app import os, shutil import tkinter as tk from tkinter import filedialog, messagebox, Tk, Canvas, Entry, Text, Button, PhotoImage from tkinter import font as tkFont def build_org_chart(): print("im making a chart") return 'Done chart created!' if __name__ == "__main__": window = Tk() window.title("Org Chart Spreadsheet Generator") # Variables to store file paths window.geometry("1012x506") window.configure(bg = "#00403D") # Define the font properties my_font = tkFont.Font(family="Montserrat SemiBold", size=16, weight="normal") canvas = Canvas( window, bg = "#00403D", height = 506, width = 1012, bd = 0, highlightthickness = 0, relief = "ridge" ) canvas.place(x = 0, y = 0) canvas.create_rectangle( 308.0, 0.0, 1012.0, 506.0, fill="#FFFFFF", outline="") canvas.create_text( 320.0, 18.0, anchor="nw", text="Org Chart", fill="#000000", font=("Montserrat Bold", 64 * -1) ) window.resizable(False, False) window.mainloop()  Also now my app is so small its still crashing im thinking it could be something in the setup file too so ive added that code below import os from setuptools import setup def list_files(directory): base_path = os.path.abspath(directory) paths = [] for root, directories, filenames in os.walk(base_path): for filename in filenames: # Exclude .DS_Store files if you are on macOS if filename != '.DS_Store': paths.append(os.path.join(root, filename)) return paths # Your assets folder assets_folder = 'assets' # Listing all files in the assets folder assets_files = list_files(assets_folder) APP = ['org_chart_min.py'] DATA_FILES = [('assets', assets_files)] OPTIONS = { 'argv_emulation': True, 'packages': ['pandas', 'openpyxl','xlsxwriter'], 'plist': { 'CFBundleName': '_org_chart', 'CFBundleDisplayName': ' Org Chart', 'CFBundleGetInfoString': "Create a spreadsheet that populates our Lucid org chart template", 'CFBundleIdentifier': 'com.yourdomain.orgchart', 'CFBundleVersion': '0.1', 'CFBundleShortVersionString': '0.1', 'NSRequiresAquaSystemAppearance': True }, 'iconfile': 'org_chart.icns', } setup( app=APP, data_files=DATA_FILES, options={'py2app': OPTIONS}, setup_requires=['py2app'], ) 

Answer: I suppose you use pyinstaller. It works for mac and it will compile an UNIX file. Just use pip install pyinstaller and after that in directory of your script pyinstaller -F main.py. Rename your file as main.py and it should compile one UNIX file. -F flag compiles it in one file. It works perfectly!
Question: I have several dictionary with equal keys. The values of the key are list. Now I would merge the values lists like. Input, e.g.: dict_1 ={"a":["1"], "b":["3"]} dict_2 = {"a":["2"], "b":["3"]}  Required output: new_dict = {'a':["1","2"], 'b':["3","3"]}  What is the fastest, pythonic way to get this result? I found this, but this doesn’t fulfill my needs: merged_dic = {**dict_1, **dict_2}  and others, but nothing solve my wish. Is there a built in function without loop over each element, because I have a lot of dictionaries and more complex as my example above? Thanks for any help!

Answer: you can use defaultdict with extend like this: from collections import defaultdict new_dict = defaultdict(list) for d in [dict_1, dict_2]: for key, value in d.items(): new_dict[key].extend(value)
Question: I have a special dataframe called df here is how it looks like RepID +Col01 +Col02 +Col03 -Col01 +Col04 +Col05 -Col03 -Col04 +Col06 -Col07 1 5 7 9 8 3 8 1 9 4 6 2 1 3 3 3 1 2 2 3 6 0 3 9 8 0 9 4 9 5 1 2 0 4 3 1 0 5 8 7 1 0 9 2 5 0 7 1 2 0 0 2 9 2 1  They are all positive numbers in the data but if you notice the column name it is a column name that is either with + or with - Some of these columns have + with no columns with the - (Such as +Col06) Some of these columns have - with no columns with the + (Such as -Col07) Some other have both (Such as +Col01 and -Col01) I want to make this dataset normalised by subtracting ting the value in the - columns from the + columns, and chnage the column name to a name with no + or - in the begining of the name, so the end table will look like this RepID Col01 Col02 Col03 Col04 Col05 Col06 Col07 1 -3 7 8 -6 8 4 -6 2 -2 3 1 -2 2 6 -0 3 0 8 -5 3 9 2 0 4 -2 1 -1 8 7 9 -2 5 -2 7 -1 -9 0 2 -1  Is there anyway I can do that

Answer: No need for groupby, just extract the + and - columns separately, subtract with fill_value=0, concat to the ID: out = pd.concat([df[['RepID']], df.filter(regex='^\+') .rename(columns=lambda x: x[1:]) .sub(df.filter(regex='^-') .rename(columns=lambda x: x[1:]), fill_value=0) ], axis=1)  Output:  RepID Col01 Col02 Col03 Col04 Col05 Col06 Col07 0 1 -3 7.0 8 -6 8.0 4.0 -6.0 1 2 -2 3.0 1 -2 2.0 6.0 0.0 2 3 0 8.0 -5 3 9.0 2.0 0.0 3 4 -2 1.0 -1 8 7.0 9.0 -2.0 4 5 -2 7.0 -1 -9 0.0 2.0 -1.0
Question: I want to calculate the difference between open and close price but taking to account if a trade is a Buy/Sell. I have the conditional function below; but l do not know how to insert a new column with the results into the original dataframe which has a list of trades and l am getting an error with this function. def pl_gap(type): if type in mt_trades['type'] == 'BUY': mt_trades['close_price'] - mt_trades['open_price'] else: mt_trades['open_price'] - mt_trades['close_price'] mt_trades['pl_gap'] = mt_trades.apply(pl_gap)

Answer: I also found out that this solution also works: mt_trades["pl_gap"] = mt_trades.apply( lambda row: ( row["close_price"] - row["open_price"] if row["type"] == "BUY" else row["open_price"] - row["close_price"] ), axis=1, )
Question: I have this constraint with big-M parameter and an auxiliary binary variable w: for i in customers: for j in customers: if i != j: mdl.addConstr(y[j] + z[j] <= y[i] + z[i] - df.demand[j]*(x1[i,j] + x2[i,j]) + 100000 * (1 - w), name= 'C8')  When I run the code, I got the following error: TypeError: unsupported operand type(s) for -: 'int' and 'tupledict'  w is defined as follows: w = mdl.addVars(0,1,vtype=GRB.BINARY, name='w')  I couldn't figure out what is the problem? Is it a problem in defining w? Thank you

Answer: w is tupledict as it was defined using mdl.addVars. As w is a single variable you need to define it using mdl.addVar instead: w = mdl.addVar(0,1,vtype=GRB.BINARY, name='w')
Question: Is there a way to generate each endpoint on its own page instead of generating the documentation for all of the endpoints on a single page like below: I would like to show, let's say, the GET endpoint for books on its own dedicated page without all the other endpoints. I want this because I am using an iframe tag to embed the UI for a specific endpoint. 

Answer: You could achieve having separate Swagger UI (OpenAPI) autodocs generated by using Sub applications. In the following example, you could access the Swagger UI autodocs for the main API at http://127.0.0.1:8000/docs, and the docs for the sub API at http://127.0.0.1:8000/subapi/docs. Example from fastapi import FastAPI app = FastAPI() @app.get("/app") def read_main(): return {"message": "Hello World from main app"} subapi = FastAPI() @subapi.get("/sub") def read_sub(): return {"message": "Hello World from sub API"} app.mount("/subapi", subapi)  If you instead would like to group/sort the endpoints in a single /docs page, please have a look at this answer.