eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I have two dataframes. df1 contains CITIES and the total number of VISITS. df2 contains the VISITS records. Periodically df1 is updated with the data from df2 with the new VISITS. df1 example (before updating) ID NAME VISITS --- 01 CITY1 01 02 CITY2 01 ... 06 CITYZ 12  df2 example CITY NUMBER --- ... CITY1 01 CITY2 01 <--- highest of CITY2 CITYZ 13 CITY1 02 ... CITYZ 14 CITY1 03 <--- highest of CITY1 CITYZ 15 <--- highest of CITYZ  To update it will look for df1['NAME'] in df2['CITY'] (this is the correlation) and take the highest df2['NUMBER'] and put it in df1['VISITS'] of that CITY. df1 after updating ID NAME VISITS --- 01 CITY1 03 <--- updated 02 CITY2 01 <--- updated or not, it doesn't matter ... 06 CITYZ 15 <--- updated  my approach: df2.loc[df2['CITY'] == 'CITYZ', 'NUMBER'].max()  I get the max number of "CITIZ" (hardcoded), but I don't know how to link it to df1. The next is clearly wrong, but it is the idea: df1['VISITS'] = df2.loc[df2['CITY'] == df1['NAME'], 'NUMBER'].max()  This "solution" gives the following error: ValueError: Can only compare identically-labeled Series objects

Answer: You can remove duplicates from df2 and keep only the last updated values (if your cities are already sorted by 'NUMBER', you can remove sort_values): df1['VISITS'] = df1['NAME'].map(df2.sort_values('NUMBER', ascending=True) .drop_duplicates('CITY', keep='last') .set_index('CITY')['NUMBER']) print(df1) # Output ID NAME VISITS 0 1 CITY1 3 1 2 CITY2 1 2 6 CITYZ 15  Based on @Nick's answer: df1['VISITS'] = df1['NAME'].map(df2.groupby('CITY')['NUMBER'].max())  Input data: >>> df1 ID NAME VISITS 0 1 CITY1 1 1 2 CITY2 1 2 6 CITYZ 12 >>> df2 CITY NUMBER 0 CITY1 1 1 CITY2 1 2 CITYZ 13 3 CITY1 2 4 CITYZ 14 5 CITY1 3 6 CITYZ 15
Question: I have this code which is suppose to look for /help command in telegram. So once you type /help in the telegram channel it will give you options. The code is as follows. from telegram import Update from telegram.ext import Updater, CommandHandler, MessageHandler, CallbackContext from telegram.ext import filters # Define your bot token here TOKEN = "YOUR_BOT_TOKEN" def start(update, context): update.message.reply_text("Welcome to your Telegram bot!") def help_command(update, context): update.message.reply_text("You requested help. Here are some available commands:\n" "/help - Show this help message\n" "/start - Start the bot") def handle_message(update, context): text = update.message.text if text == '/start': start(update, context) elif text == '/help': help_command(update, context) def main(): # Initialize the Updater with your bot token updater = Updater(token=TOKEN, use_context=True) dispatcher = updater.dispatcher # Define the command handlers dispatcher.add_handler(CommandHandler("start", start)) dispatcher.add_handler(CommandHandler("help", help_command)) # Handle non-command messages using a filter dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, handle_message)) # Start the bot updater.start_polling() updater.idle() if __name__ == '__main__': main()  However I am getting this error TypeError: Updater.__init__() got an unexpected keyword argument 'token'  Could you please advise how I can resolve this error.

Answer: I used telegram.Bot class and then I passed it to the Updater like this: from telegram import Update, Bot from telegram.ext import Updater, CommandHandler, MessageHandler, CallbackContext from telegram.ext import filters #defining your bot token here TOKEN = "YOUR_BOT_TOKEN" def start(update: Update, context: CallbackContext): update.message.reply_text("Welcome to your Telegram bot!") def help_command(update: Update, context: CallbackContext): update.message.reply_text("You requested help. Here are some available commands:\n" "/help - Show this help message\n" "/start - Start the bot") def handle_message(update: Update, context: CallbackContext): text = update.message.text if text == '/start': start(update, context) elif text == '/help': help_command(update, context) def main(): #creating a bot instance bot = Bot(token=TOKEN) #creating an updater instance and pass the bot instance updater = Updater(bot=bot, use_context=True) dispatcher = updater.dispatcher #defining the command handlers dispatcher.add_handler(CommandHandler("start", start)) dispatcher.add_handler(CommandHandler("help", help_command)) #handling non-command messages using a filter dispatcher.add_handler(MessageHandler(filters.Text & ~filters.Command, handle_message)) #starting the bot updater.start_polling() updater.idle() if __name__ == '__main__': main()
Question: I tried to use the CI_JOB_TOKEN in a GitLab CI pipeline to install a Python Package from a different project's package registry. According to the documentation i should just have to add my project to the allowlist of the corresponding project and run the pipeline. However i always get the following 401 error when running this command pip install --extra-index-url https://__token__:$CI_JOB_TOKEN@gitlab.com/api/v4/projects/<projId>/packages/pypi/simple <package>  Looking in indexes: https://pypi.org/simple, https://__token__:****@gitlab.com/api/v4/projects/<projId>/packages/pypi/simple WARNING: 401 Error, Credentials not correct for https://gitlab.com/api/v4/projects/<projId>/packages/pypi/simple/<package>/ ERROR: Could not find a version that satisfies the requirement <package> (from versions: none) ERROR: No matching distribution found for <package> WARNING: 401 Error, Credentials not correct for https://gitlab.com/api/v4/projects/<projId>/packages/pypi/simple/pip/  I tried to remove the allow list protections completely and it still did not work. As a workaround i just added a secret variable in form of an Access Token to the pipelines in the GUI and with that it works, but this seems rather hacky since i need to update the token every few weeks. Did i miss anything in the documentation and the CI_JOB_TOKEN does not have access to these registries ?

Answer: According to the GitLab PyPI registry authentication documentation, you should use the username gitlab-ci-token when authenticating with a job token. This might be confusing because some other examples use __token__ even though GitLab does not accept this username unless you are using an access token literally named __token__. __token__ is normally used for tokens on PyPI.org, however.
Question: I have a code to plot a heatmap for a set of data, represented as (x, y, f(x, y)), and I want to find the local minimum points. import numpy as np import math import matplotlib.pyplot as plt import matplotlib as mpl from scipy.interpolate import griddata data = np.genfromtxt('data.dat', skip_header=1, delimiter=' ') x, y, z = data[:, 1], data[::, 0], data[:, 2] x, y, z = x*180/math.pi, y*180/math.pi, z - min(z) xi, yi = np.linspace(max(x), min(x), 1000), np.linspace(max(y), min(y), 1000) xi, yi = np.meshgrid(xi, yi) zi = griddata((x, y), z, (xi, yi), method='linear') plt.figure(figsize=(10,5)) plt.pcolormesh(xi, yi, zi, shading='auto', cmap='jet') plt.colorbar(label='Heatmap') plt.gca().invert_yaxis() plt.show()  Here's code to generate some fake data: import math with open('data.dat', 'w') as arquivo: for x in range(20): for y in range(20): z = -math.exp(math.sin(x*y)- math.cos(y)) arquivo.write(f"{x}\t\t{y}\t\t{z}\n")  Heatmap example with minimum points circled:  I tried to use np.gradient, thinking that maybe by taking two derivatives I would be able to determine the local minimum points (zero in the first derivative and negative in the second), but I was not able to make any of it work.

Answer: To find local minima, we usually used some gradient based optimizations like gradient descent. However, it is not easy to find all local minima unless doing a lot of "restart" (generally people are happy with one local minimum). One straightforward method to your problem is using grid search: if the current point is less than the neighbor around it, it is one local minimum. The code snippet is below # Function to get the neighbors of a given point (i,j) def get_neighbors(i, j, shape): neighbors = [] for x in [-1, 0, 1]: for y in [-1, 0, 1]: ni, nj = i + x, j + y if (0 <= ni < shape[0]) and (0 <= nj < shape[1]) and (x, y) != (0, 0): neighbors.append((ni, nj)) return neighbors local_minima = [] # Iterate over the 2D grid for i in range(zi.shape[0]): for j in range(zi.shape[1]): current_value = zi[i, j] neighbors = get_neighbors(i, j, zi.shape) # Check if the current point is less than all its neighbors if all(current_value < zi[n[0], n[1]] for n in neighbors): local_minima.append((xi[i, j], yi[i, j], current_value)) # Print the local minima for loc in local_minima: print(f"Local minimum value {loc[2]} at location ({loc[0]}, {loc[1]}).")  And then plot the local minima # Marking all the local minima on the plot for loc in local_minima: plt.scatter(loc[0], loc[1], color='red', s=100, marker='x')
Question: I wanted to convert a list to pandas dataframe, where the first element of the list is a dictionary. I have below code import pandas as pd import numpy as np pd.DataFrame([{'aa' : 10}, np.nan])  However this fails with below message Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python3.11/site-packages/pandas/core/frame.py", line 782, in __init__ arrays, columns, index = nested_data_to_arrays( ^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/pandas/core/internals/construction.py", line 498, in nested_data_to_arrays arrays, columns = to_arrays(data, columns, dtype=dtype) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/pandas/core/internals/construction.py", line 832, in to_arrays arr, columns = _list_of_dict_to_arrays(data, columns) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/pandas/core/internals/construction.py", line 912, in _list_of_dict_to_arrays pre_cols = lib.fast_unique_multiple_list_gen(gen, sort=sort) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "pandas/_libs/lib.pyx", line 374, in pandas._libs.lib.fast_unique_multiple_list_gen File "/usr/local/lib/python3.11/site-packages/pandas/core/internals/construction.py", line 910, in <genexpr> gen = (list(x.keys()) for x in data) ^^^^^^ AttributeError: 'float' object has no attribute 'keys'  Could you please help how to resolve this issue?

Answer: Enclose your list into np.array: pd.DataFrame(np.array([{'aa' : 10}, np.nan]))    0 0 {'aa': 10} 1 NaN   Though you list is quite small, here's timings comparison just for the case: In [777]: %timeit pd.DataFrame(np.array([{'aa' : 10}, np.nan])) 26.6 µs ± 220 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each) In [778]: %timeit pd.Series([{'aa' : 10}, np.nan]).to_frame() 49.6 µs ± 911 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
Question: When I have an array a and a boolean mask b, I can find the 'masked' vector c. a = np.array([1, 2, 4, 7, 9]) b = np.array([True, False, True, True, False]) c = a[b]  Now suppose, it's the other way around. I have c and b and would like to arrive at d (below). What is the easiest way to do this? c = np.array([1, 4, 7]) b = np.array([True, False, True, True, False]) d = np.array([1, 0, 4, 7, 0]) 

Answer: You could use: d = np.zeros_like(b, dtype=c.dtype) d[b] = c  Output: array([1, 0, 4, 7, 0])
Question: I would like to disable automatic font colouring of negetive values in polars write_excel. Any tip? import polars as pl import xlsxwriter df2 = pl.DataFrame(data=np.random.randint(-10, 10, 5*3).reshape(-1, 3), schema=['x', 'y', 'z']) with xlsxwriter.Workbook(r'_out_.xlsx') as workbook: df2.write_excel(workbook=workbook, worksheet='sheet1', autofit=True, table_style=None, column_formats=None, conditional_formats=False, float_precision=0, sparklines=None, formulas=None)  This is what it generates - 

Answer: column_formats={cs.numeric():'General'} or column_formats={x:'[Black]' for x in df2.columns} will do the trick. import numpy as np import polars as pl import polars.selectors as cs import xlsxwriter df2 = pl.DataFrame(data=np.random.randint(-10, 10, 5*3).reshape(-1, 3), schema=['x', 'y', 'z']) with xlsxwriter.Workbook(r'C:\SUVOPAM_local\Try\_out_.xlsx') as workbook: df2.write_excel(workbook=workbook, worksheet='sheet1', autofit=True, table_style=None, dtype_formats=None, column_formats={cs.numeric():'General'}, # column_formats={x:'[Black]' for x in df2.columns} conditional_formats=False, float_precision=0, sparklines=None, formulas=None)  Generates -
Question: I am using the Text widget in python / tkinter. I want to use the left and right option for marks so that text inserted immediately before or immediately after already tagged text is in the same tag range. In the code below, I don’t understand why inserted text does not appear in red. import tkinter as tk main = tk.Tk() tt = tk.Text(main, width=50, height=5, bg='lightgray') tt.pack() # some text tt.insert('1.0', 'STRANGE!') # inserting marks in text tt.mark_set('redLeft', '1.2') tt.mark_gravity('redLeft', 'left') tt.mark_set('redRight', '1.6') tt.mark_gravity('redRight', 'right') # a tag for red text tt.tag_add('red', 'redLeft', 'redRight') tt.tag_configure('red', foreground='red') # insertions after, in and before red text tt.insert('1.6', 'should be red ?') tt.insert('1.4', 'is red') tt.insert('1.2', 'should be red ?') main.mainloop()  Thank you for any help.

Answer:  I don’t understand why inserted text does not appear in red.  This is how the text widget is designed to work. The gravity of a mark does not affect the tags that are applied when inserting text. The gravity only defines what happens to the mark when text is inserted at the mark. From the canonical documentation on marks: "The gravity for a mark specifies what happens to the mark when text is inserted at the point of the mark. If a mark has left gravity, then the mark is treated as if it were attached to the character on its left, so the mark will remain to the left of any text inserted at the mark position. If the mark has right gravity, new text inserted at the mark position will appear to the left of the mark (so that the mark remains rightmost)." When you insert text, the text will only inherit tags if the tag is on the character both to the left and the right of the insertion point. From the canonical documentation on the insert method: "If there is a single chars argument and no tagList, then the new text will receive any tags that are present on both the character before and the character after the insertion point; if a tag is present on only one of these characters then it will not be applied to the new text."
Question: import unittest from appium import webdriver from appium.webdriver.common.appiumby import AppiumBy capabilities = dict( platformName='Android', automationName='uiautomator2', deviceName='Samsung S9', appPackage='com.android.settings', appActivity='.Settings', language='en', locale='US' ) appium_server_url = 'http://localhost:4723' class TestAppium(unittest.TestCase): def setUp(self) -> None: self.driver = webdriver.Remote(appium_server_url, capabilities) def tearDown(self) -> None: if self.driver: self.driver.quit() def test_find_battery(self) -> None: el = self.driver.find_element(by=AppiumBy.XPATH, value='//*[@text="Battery"]') el.click() if __name__ == '__main__': unittest.main()  The above is the example code from official Appium website (http://appium.io/docs/en/2.1/quickstart/test-py/), I have installed all the prequisites requred but still I'm getting the below error when I run the python file: C:\Users\syeda\Desktop>python test.py E ====================================================================== ERROR: test_find_battery (__main__.TestAppium.test_find_battery) ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\Users\syeda\Desktop\test.py", line 19, in setUp self.driver = webdriver.Remote(appium_server_url, capabilities) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\syeda\AppData\Local\Programs\Python\Python311\Lib\site-packages\appium\webdriver\webdriver.py", line 229, in __init__ super().__init__( File "C:\Users\syeda\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 185, in __init__ capabilities = options.to_capabilities() ^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'to_capabilities' ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (errors=1)  I made sure the Appium server is running. I'm not sure why this eroor is occuring. I tried searching on the web but no luck.

Answer: import unittest from appium import webdriver from appium.webdriver.common.appiumby import AppiumBy # Import Appium UiAutomator2 driver for Android platforms (AppiumOptions) from appium.options.android import UiAutomator2Options capabilities = dict( platformName='Android', automationName='uiautomator2', deviceName='Samsung S9', appPackage='com.android.settings', appActivity='.Settings', language='en', locale='US' ) appium_server_url = 'http://localhost:4723' # Converts capabilities to AppiumOptions instance capabilities_options = UiAutomator2Options().load_capabilities(capabilities) class TestAppium(unittest.TestCase): def setUp(self) -> None: self.driver = webdriver.Remote(command_executor=appium_server_url,options=capabilities_options) def tearDown(self) -> None: if self.driver: self.driver.quit() def test_find_battery(self) -> None: el = self.driver.find_element(by=AppiumBy.XPATH, value='//*[@text="Battery"]') el.click() if __name__ == '__main__': unittest.main()
Question: I'm trying to print out a diamond shape using star characters and additional characters using python. I tried a lot to get the result I wanted, but not successful. I want to write a function that receives a parameters and according to that parameter it should determine the height of the diamond shape. For example : If the parameter is 6, the first half must have 5 lines except for the middle line. middle line is shared by both the halves. bottom half must also have 5 lines. But the additional requirement is after first 2 lines, instead of * characters, it must have "1 # 1" in the first half and in the second half it must have "2 # 2" For example, if the input is 6, it must produce some like below  * *** 1 # 1 1 # 1 1 # 1 *********** 2 ^ 2 2 ^ 2 2 ^ 2 *** *  So, this is what I tried so far def print_half_diamond(height): if height <= 0: print("Height must be a positive integer.") return for i in range(1, height + 1): spaces = " " * (height - i) line = "" if i == 1 or i == 2: line = "*" * (2 * i - 1) else: line = "1" + " " * (2 * (i - 2) + 1) + "#" + " " * (2 * (height - i)) + "1" print(spaces + line) print("*" * (2 * height - 1)) for i in range(height - 1, 0, -1): spaces = " " * (height - i) stars = "*" * (2 * i - 1) print(spaces + stars) print_half_diamond(6)  The first half is somewhat correct but the spacing is wrong, the second half just prints out the * characters as follows  * *** 1 # 1 1 # 1 1 # 1 1 #1 *********** ********* ******* ***** *** *  How do i get the result I want? Help is much appreciated.

Answer: The top and bottom halves of the diamond are effectively mirror images of each other. Therefore you only need to write code to format one half. The other half is just the reverse. Something like this: def diamond(n): def rows(c, mc='#'): return [f'{c: >{i+2}}{mc: >{m-i-1}}{c: >{m-i-1}}' for i in range(n-3)] if n > 2: middle = '*' * (w := n * 2 - 1) m = w // 2 top = [f'{"***": >{m+2}}', f'{"*": >{m+1}}'] print(*reversed(rows('1')+top), middle, *rows('2', '^')+top, sep='\n') diamond(6)  Output:  * *** 1 # 1 1 # 1 1 # 1 *********** 2 ^ 2 2 ^ 2 2 ^ 2 *** *  Or, by using the centering format specifier... def diamond(n): def rows(c, mc='#'): return [f'{e:^{w}}' for e in [f'{c}{" "*i}{mc}{" "*i}{c}' for i in range(n-3, 0, -1)] + ['***', '*']] if n > 2: w = n * 2 - 1 print(*reversed(rows('1')), '*'*w, *rows('2', '^'), sep='\n')
Question: I'm trying to match an entire string that starts with a certain string and then match any number of characters except ::, if :: was matched then only accept if followed by the string CASE. So for example: A string that starts with Linus:: followed by 0 or more 1 characters except if :: then CASE has to follow else only matches everything before the ::. Linus::AOPKNS::CASE would capture the entire string Linus::AOPKNS would capture the entire string Linus::AOPKNS::OK would only capture Linus::AOPKNS I imagine I'd have to use a positive lookahead but I'm not quite sure how to do that considering I wanna match any number of characters before the ::.

Answer: Use a tempered greedy token: ^ # Match at the start of the string Linus:: # 'Linus::', literally, (?:(?!::).)+ # followed by a sequence of characters that doesn't contain '::' (?:::CASE)? # and, optionally, '::CASE'.  Try it on regex101.com. Depending on your use case, you might want to add a \b (word boundary) at the end of the pattern.
Question: I was going to ask if there is a pylint-style code analyzer capable of suggesting the use of the := operator in places were it might improve the code. However, it looks like such test has been added to the pylint two years ago -> github PR (merged). Anyway I never saw such suggestion, not even for this example like in the linked PR: x = 2 if x: print(x) # ----- # if (x := 2): # print(x) # -----  This feature is available since Python 3.8. (I'm using recent Python and pylint versions.) I though I have to enable it somehow, but the help says:  --py-version <py_version> Minimum Python version to use for version dependent checks. Will default to the version used to run pylint.  What is wrong? Why there is no consider-using-assignment-expr from pylint?

Answer: The consider-using-assignment-expr check in pylint can be enabled by Adding the following line to your pylint configuration file. I am using a configuration file named pylint.toml: [tool.pylint.main] load-plugins="pylint.extensions.code_style"  Then you can run the linter using pylint --rcfile <config_file> <python_file>. See here for more instructions. Note that I am using Python 3.11, and Pylint 2.17, but the check should be available since Python 3.8.
Question: Let A and B two dataframes columns:     Hello Foo Hey Bar World        Hello Bar Doo World Star     I want to obtain the columns of a dataframe C containing all the unique columns in their concatenation BUT the columns must be in the same order as before.     Hello Foo Bar Hey Doo World Star     In other words: "If A is an older version of a dataframe and B is a newer, how to get a dataframe C which keeps track of A deleted columns (not present in B) and B added columns (not present in A) in a way that keeps the ordering consistent with A (or B)?". The method align can mix the two dataframe columns but it does not preserve the original order, the order is in lexicographical order.

Answer: Assuming you really want to keep the original order in both Indexes (and assuming there is no circular pattern), you can use the following algorithm: A = pd.DataFrame(columns=['Hello', 'Foo', 'Hey', 'Bar', 'World']) B = pd.DataFrame(columns=['Hello', 'Bar', 'Doo', 'World', 'Star']) def merge(A, B): sA = set(A) sB = set(B) out = {} while sA or sB: # while items are left in A or B for a in A: # take from A while not in B sA.discard(a) if a in sB: break out.setdefault(a) for b in B: # take from B while not in A sB.discard(b) if b in sA: break out.setdefault(b) return list(out) out = merge(A, B)  Output: ['Hello', 'Foo', 'Hey', 'Bar', 'Doo', 'World', 'Star']  generic solution Alternatively, you can use graph theory, construct a directed graph with all the pairs of successive columns as edges using networkx.from_edgelist and itertools.pairwise, then find the longest path with dag_longest_path: import networkx as nx from itertools import pairwise dfs = [A, B] G = nx.from_edgelist((pair for pairs in map(pairwise, dfs) for pair in pairs), create_using=nx.DiGraph) C_cols = nx.dag_longest_path(G)  Output: ['Hello', 'Foo', 'Hey', 'Bar', 'Doo', 'World', 'Star']  Graph: