eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: This Python 3.12.7 script with numpy 2.2.4: import numpy as np a = np.random.randint(0, 256, (500, 500)).astype(np.uint8) counts, bins = np.histogram(a, range(0, 255, 25)) print(np.column_stack((counts, bins[:-1], bins[1:]))) counts, bins = np.histogram(a, range(0, 257, 16)) print(np.column_stack((counts, bins[:-1], bins[1:])))  produces this kind of output: [[24721 0 25] [24287 25 50] [24413 50 75] [24441 75 100] [24664 100 125] [24390 125 150] [24488 150 175] [24355 175 200] [24167 200 225] [25282 225 250]] [[15800 0 16] [15691 16 32] [15640 32 48] [15514 48 64] [15732 64 80] [15506 80 96] [15823 96 112] [15724 112 128] [15629 128 144] [15681 144 160] [15661 160 176] [15558 176 192] [15526 192 208] [15469 208 224] [15772 224 240] [15274 240 256]]  where the first histogram always has the highest count in bin [225, 250). The second histogram indicates a uniform distribution, as expected. I tried a dozen of times and the anomaly was always there. Can someone explain this behavior?

Answer: I think the docs explain pretty well what's happening, but are spread out in two different places. First, the range range(0, 255, 25) is supplying the bins parameter, not the range parameter. Secondly, the Notes section states:  All but the last (righthand-most) bin is half-open. In other words, if bins is: [1, 2, 3, 4]  then the first bin is [1,2) (including 1, but excluding 2) and the second [2,3). The last bin, however, is [3,4], which includes 4.  Pretty sure the extra counts in your case are the number of elements that equal 250. This makes sense, since the increase is about 1/25th of the bin size compared to the other bins, which all have a width of 25.
Question: I have a list containing thousands of survey answers ranging from 'A' to 'I', with the addition of None for blanks: Raw data list example: [None, 'A', 'G', None, None, 'I', 'B', ...<snip>... , 'C']  I would like to produce a Seaborn histogram showing the frequency of each response (in alphabetical order), something similar to this Excel chart: Desired outcome (Excel version)  (Numbers are different here than in test data below) The closest I get is this, with all columns incorrectly equal to 1:  I tried histplot, catplot and displot as per some random tutorials online. I am unsure of how to process the list of raw values into the right format for Seaborn. I have followed many different tutorials doing things like sns.histplot(data=df, x="Frequency") but can't get the dataframe right before this step. Main method I tried for frequency (from some tutorials): df = pd.DataFrame.from_dict(Counter(values), orient='index').reset_index() Any suggestions will be humbly received. Much appreciation in advance. Minimum reproducible example: import matplotlib.pyplot as plt from collections import Counter import seaborn as sns import pandas as pd # Test data - real data is much larger and extracted as a list from an SQLite3 DB. values = ['E', None, 'B', 'H', 'I', 'A', None, None, 'D', 'C', 'E', 'I', 'C', 'B', None, 'A', None, 'E', 'F', 'H', 'A', 'D', 'A', 'A', 'F', 'A', 'C', 'C', 'H', 'E', None, 'B', 'E', 'I', 'G', 'A', 'I', 'A', 'B', 'I'] sns.set_theme(style="darkgrid") df = pd.DataFrame.from_dict(Counter(values), orient='index').reset_index() df.columns = ['Choice', 'Frequency'] sns.histplot(data=df, x='Choice') plt.show() 

Answer: Since you have pre-aggregated data, you should use a barplot, not a histplot that is designed to perform the aggregation. For the alphabetical order, use sort_index: df = (pd.DataFrame.from_dict(Counter(values), orient='index') .sort_index().reset_index() ) df.columns = ['Choice', 'Frequency'] sns.barplot(data=df, x='Choice', y='Frequency')  Output:  Note that you do not need to use a Counter/DataFrame, you could directly go with a countplot: sns.countplot(values)  Output:
Question: I would like to create a chart on a powerpoint slide with pourcent datas , python doesn't let my directly put the % symbol on my data so i had to only put the datas , but i want to show the % symbol on the chart and also on the axes So heres my code :  if slide: for shape in slide.shapes: if shape.shape_type == MSO_SHAPE_TYPE.CHART: chart = shape.chart if result and moisDate and nameIntake : chart_data = CategoryChartData() chart_data.categories = nameIntake for moisSingleDate in moisDate: chart_data.add_series( str(moisSingleDate) , values= ( result[moisSingleDate]) ,number_format= '0%') chart.replace_data(chart_data) print(result)  but unfortunatly my datas gets multiplied by 100... instead of displaying 24.93% i get 2493% does anyone knows how to do it please ?

Answer: You have to divide by 100 in-place. Percentage formatting expects decimal values normalized to 0.0..1.0 as 0..100%. for moisSingleDate in moisDate: # Divide values by 100 to correctly format as percentages percentage_values = [value / 100 for value in result[moisSingleDate]] chart_data.add_series(str(moisSingleDate), values=percentage_values, number_format='0%')
Question: I have created the following array, called X: array([[6.575], [6.421], [7.185], [6.998], [6.43 ], [6.012], [6.172], [5.631], [6.004], [6.377], [6.03 ]])  and I would like to create array([[6.575, 1], [6.421, 1], [7.185, 1], [6.998, 1], [6.43, 1], [6.012, 1], [6.172, 1], [5.631, 1], [6.004, 1], [6.377, 1], [6.03, 1]])  I have tried X = np.array( [ [value,1] for value in X ] ) but I get the error: ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (506, 2) + inhomogeneous part. Aside from ignorance I am not sure where I am going wrong. Any ideas?

Answer: Do not use list comprehensions/loops to work with numpy arrays. You should combine expand the scalar with broadcast_to and combine it to X with hstack: out = np.hstack([X, np.broadcast_to(1, X.shape)])  Output: array([[6.575, 1. ], [6.421, 1. ], [7.185, 1. ], [6.998, 1. ], [6.43 , 1. ], [6.012, 1. ], [6.172, 1. ], [5.631, 1. ], [6.004, 1. ], [6.377, 1. ], [6.03 , 1. ]])  Now, just to answer the source of your error, this this because your intermediate list comprehension generates items of the form [array([6.575]), 1] instead of [6.575, 1]. The correct approach should have been: np.array([[value, 1] for value in X[:, 0]]) # or np.array([[*value, 1] for value in X])  But, again, do not do this in numpy.
Question: I have to create a plot on which ticks and labels are specifically defined; an example of reproducible plot is given below: import matplotlib.pyplot as plt import seaborn as sns plt.style.use('seaborn-v0_8') fig, ax = plt.subplots(figsize=(4, 4)) ticks = [0.00, 0.25, 0.50, 0.75, 1.00] ax.set_xticks(ticks) ax.set_xticklabels(ticks, weight='bold', size=8) ax.set_yticks(ticks) ax.set_yticklabels(ticks, weight='bold', size=8) plt.show()  As ticks and labels are exactly the same of both axes, is there a way to set them as a single command ? Something mixing both xticks and yticks ?

Answer: You can use a function. import matplotlib.pyplot as plt plt.style.use('seaborn-v0_8') def set_ticks_and_labels(ax, t, **kwargs): ax.set_xticks(t) ax.set_xticklabels(t, **kwargs) ax.set_yticks(t) ax.set_yticklabels(t, **kwargs) fig, ax = plt.subplots(figsize=(4, 4)) ticks = [0.00, 0.25, 0.50, 0.75, 1.00] set_ticks_and_labels(ax, ticks, weight='bold', size=8) plt.show()
Question: In the example below, everything is already processed using isort . (version 6.0.1): tree .  . ├── a │ └── test │ ├── __init__.py │ └── test_integration.py └── b └── tests ├── __init__.py └── test_integration.py  cat ./a/test/test_integration.py  from test.asdasd import hello import numpy as np if np.random.random() < 42: hello()  cat ./b/tests/test_integration.py  import numpy as np from tests.asdasd import hello if np.random.random() < 42: hello()  Why does isort put from test.asdasd import hello (in a) above import numpy as np, while it puts from tests.asdasd import hello (in b) below import numpy as np?

Answer: The module name test clashes with the standard library module test. isort will, by default, order with the following priorities ('FUTURE', 'STDLIB', 'THIRDPARTY', 'FIRSTPARTY', 'LOCALFOLDER')  As test is within isort's list of known standard library names, it get's prioritised in the ordering, whereas tests doesn't. You can force isort to see test as, e.g., a local folder, and sort things consistently by using a config file with the known_local_folder set appropriately. E.g., using a .isort.cfg configuration file with: [settings] known_local_folder=test  or in a pyproject.toml file with: [tool.isort] known_local_folder = ["test"]
Question: I am new to Python and I want to check whether the uploaded PDF file contains all the required signatures. If any are missing, it should return "Signature missing." I’ve tried reading the file, but I’m not sure how to implement this check. try: images = convert_from_path(pdf_path, dpi=200) print(f"[DEBUG] PDF has {len(images)} pages") if len(images) >= 1: target_page_index = 0 # TEMP: Try first page (adjust as needed) page_image = images[target_page_index] page_image.save("debug_page0.png") # Debug test crop test_box = (100, 100, 300, 200) crop_test = page_image.crop(test_box) crop_test.save("debug_crop_test.png") # Real signature areas — update these after checking debug image inspector_sig_area = (510, 590, 650, 630) reviewer_sig_area = (510, 640, 650, 680) inspector_crop = page_image.crop(inspector_sig_area) reviewer_crop = page_image.crop(reviewer_sig_area) inspector_crop.save("debug_inspector_box.png") reviewer_crop.save("debug_reviewer_box.png") fields["Inspector Signed"] = is_signature_box_filled(inspector_crop) fields["Reviewer Signed"] = is_signature_box_filled(reviewer_crop) else: print("[!] No pages found in PDF!") except Exception as e: print(f"[!] Signature box check failed: {e}")  

Answer: As you already have your signature boxes cropped from the image, it boils down do checking for any content. Here are two variants, which can do the job. Both assume the cropped_image to be a PIL.Image. The first method checks for whiteness of the box. You can adapt the white_threshold for pixels considered to be white. You can also look for a minimum_count of non-white pixels, as any signature should have more than one pixel set. def is_signature_box_filled(cropped_image, minimum_count=0, white_threshold=220): """ Check if the signature box is filled with a signature (not all white). """ pixel_data = np.array(cropped_image.convert("L")) # Count whether there are pixels that are non-white -> signature present return np.sum(pixel_data < white_threshold) > minimum_count  The second method checks the uniformity of the box. Any signature with large enough contrast would lead to a strong non-uniformity. You can control the minimum desired contrast by setting the uniformity_threshold. def is_signature_box_filled(cropped_image, uniformity_threshold=30): """ Check if the signature box is filled with a signature (not all uniform). """ pixel_data = np.array(cropped_image.convert("L")) # Check if the pixel values are non-uniform -> signature present return (np.max(pixel_data) - np.min(pixel_data) > uniformity_threshold)
Question: from google.generativeai import error in the above import of the google's generative ai of the python module is not working as expected the error module is not found but in the below import, the error module is working prefect in my previous verison of my code, i want to know, is that deprecated or else me using wrongly the import module "from google.genai import error"

Answer: google.generativeai = Old deprecated SDK google.genai = New unified SDK https://ai.google.dev/gemini-api/docs/migrate pip uninstall google-generativeai  Then pip install google-genai  You can check available modules like so. from google import genai print("Available in main genai module:") print([item for item in dir(genai) if not item.startswith('_')])  It will give something like: ['Client', 'batches', 'caches', 'chats', 'client', 'errors', 'files', 'live', 'live_music', 'models', 'operations', 'pagers', 'tokens', 'tunings', 'types', 'version']  So correct import should be from google.genai import errors
Question: Method parameters should be contravariant, hence defining a covariant generic should raise a type error. However when using a covariant generic in a union pyright, mypy and pyre-check all do not report an error on the following code: from typing import TypeVar, Generic, Any T_co = TypeVar("T_co", covariant=True) class Foo(Generic[T_co]): def a(self, arg: T_co) -> Any: # Error, like expected as T_co is covariant. ... def b(self, arg: int | T_co) -> Any: # No error, expected one ...  As all these type-checkers do not raise an error I wonder is this actually fine, shouldn't this also break type-safety? If it is fine can you explain to why, what differs from a pure covariant implementation that is definitely not safe, shouldn't I be able to break it as well? Or if its not safe, is there an explanation why all type-checkers have the same gap here?

Answer: It appears to be a missing feature.  In Mypy's case, there's a comment about this: elif isinstance(arg_type, TypeVarType): # Refuse covariant parameter type variables # TODO: check recursively for inner type variables if ( arg_type.variance == COVARIANT and defn.name not in ("__init__", "__new__", "__post_init__") and not is_private(defn.name) # private methods are not inherited ): ... self.fail(message_registry.FUNCTION_PARAMETER_CANNOT_BE_COVARIANT, ctx)  It was added in 2016 and has been there ever since.  Pyright has a similar check, which originally prevented invalid nested types as well: if (this._containsCovariantTypeVar(paramType, node.id, diag)) { this._evaluator.addDiagnostic( this._fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, // ... ); }  This check was then changed a month later, to: if (isTypeVar(paramType) && paramType.details.isCovariant && !paramType.details.isSynthesized) { this._evaluator.addDiagnostic( this._fileInfo.diagnosticRuleSet.reportGeneralTypeIssues, // ... ); }  The commit message specifically says that such unions would be allowed, but does not give a reason:  Made the check less strict for the use of covariant type vars within a function input parameter annotation. In particular, unions that contain covariant type vars are now permitted.
Question: Consider the dataframe df: import pandas as pd df = pd.DataFrame({'Event1': ['Music', 'Something else 1', 'Theatre', 'Comedy'], 'Event2': ['Something else 2', 'Ballet', 'Something else 3', 'Something else 4'], 'Cost': [10000, 5000, 15000, 2000]}) print(df) Event1 Event2 Cost 0 Music Something else 2 10000 1 Something else 1 Ballet 5000 2 Theatre Something else 3 15000 3 Comedy 4 2000  I would like to map the values of the Event1 and Event2 to the values in the respective dictionaries:  # Mapping tables dict1 = {'Music': 'M', 'Cooking': 'C', 'Theatre': 'T', 'Comedy': 'C'} dict2 = {'Ballet': 'B', 'Swimming': 'S'}  And store the mappings in a common column because I know that per row, only the value of one column will be mapped. The end result would be: # desired outcome result = pd.DataFrame({'Event1': ['Music', 'Something else 1', 'Theatre', 'Comedy'], 'Event2': ['Something else 2', 'Ballet', 'Something else 3', '4'], 'Event': ['M', 'B', 'T', 'C'], 'Cost': [10000, 5000, 15000, 2000]}) print(result) Event1 Event2 Event Cost 0 Music Something else 2 M 10000 1 Something else 1 Ballet B 5000 2 Theatre Something else 3 T 15000 3 Comedy 4 C 2000  I can only do this in a messy and lengthy way and was hoping there is clean maybe idiomatic way of doing this. How would you advise doing it?

Answer: You can combine map and fillna: df['Event'] = df['Event1'].map(dict1).fillna(df['Event2'].map(dict2))  Output:  Event1 Event2 Cost Event 0 Music Something else 2 10000 M 1 Something else 1 Ballet 5000 B 2 Theatre Something else 3 15000 T 3 Comedy Something else 4 2000 C  If you imagine a more complex input with an arbitrary number of columns, you could generalize with a dictionary to pair the columns/dictionaries, then: dict3 = {'Comedy': 'C'} dicts = {'Event1': dict1, 'Event2': dict2, 'Event3': dict3} df['Event'] = (df[list(dicts)].apply(lambda s: s.map(dicts[s.name])) .bfill(axis=1).iloc[:, 0] )  Output:  Event1 Event2 Event3 Cost Event 0 Music Something else 2 Something else 10000 M 1 Something else 1 Ballet Something else 5000 B 2 Theatre Something else 3 Something else 15000 T 3 Something else Something else 4 Comedy 2000 C  The above approaches suppose that the mappings are per column, if you want global mappings, you can merge the dictionaries and use: from collections import ChainMap df['Event'] = (df.filter(like='Event').stack() .map(dict(ChainMap(dict1, dict2, dict3))) .groupby(level=0).first() ) # or from collections import ChainMap df['Event'] = (df.filter(like='Event') .map(dict(ChainMap(dict1, dict2, dict3)).get) .bfill(axis=1).iloc[:, 0] )
Question: I am in a situation where I have a data frame with X and X values as well as two groups GROUP1 and GROUP2. Looping over both of the groups, I want to fit a linear model against the X and Y data and the subtract the fit from the true data to get a residual. I'm currently implementing this in the following way: import polars as pl import numpy as np # --- Sample DataFrame for demonstration purposes df = pl.DataFrame( { "GROUP1": [1, 1, 1, 2, 2, 2], "GROUP2": ["A", "A", "A", "B", "B", "B"], "X": [0.0, 1.0, 2.0, 0.0, 1.0, 2.0], "Y": [5.0, 7.0, 9.0, 3.0, 4.0, 6.0], } ) # --- Function to subtract linear best fit per group def subtract_linear_best_fit(df: pl.DataFrame) -> pl.DataFrame: result = [] for _, subdf in df.group_by(["GROUP1", "GROUP2"]): x = subdf["X"].to_numpy() y = subdf["Y"].to_numpy() a, b = np.polyfit(x, y, 1) residuals = y - (a * x + b) result.append(subdf.with_columns(pl.Series("residual", residuals))) return pl.concat(result) # --- Apply function df_with_residuals = subtract_linear_best_fit(df) print(df_with_residuals)  But this does not seem nice as it does not make use of .group_by(...).agg(...) or .with_columns((...).over(...)). I tried both these approaches but I either lost columns from the original data frame or just computed a summary. But I want to have a data frame of the same height, just with one more column. Is there any way to avoid concatenating data frames inside the loop? Ideally there would be something like .group_by().pipe() or .pipe().over().

Answer: You can use a Struct to send multiple columns to .map_batches() def subtract_linear_best_fit(s: pl.Series) -> pl.Series: x, y = s.struct.unnest() a, b = np.polyfit(x, y, 1) return y - (a * x + b) df.with_columns( pl.struct("X", "Y").map_batches(subtract_linear_best_fit) .over("GROUP1", "GROUP2") .alias("residual") )  shape: (6, 5) ┌────────┬────────┬─────┬─────┬─────────────┐ │ GROUP1 ┆ GROUP2 ┆ X ┆ Y ┆ residual │ │ --- ┆ --- ┆ --- ┆ --- ┆ --- │ │ i64 ┆ str ┆ f64 ┆ f64 ┆ f64 │ ╞════════╪════════╪═════╪═════╪═════════════╡ │ 1 ┆ A ┆ 0.0 ┆ 5.0 ┆ -1.7764e-15 │ │ 1 ┆ A ┆ 1.0 ┆ 7.0 ┆ -1.7764e-15 │ │ 1 ┆ A ┆ 2.0 ┆ 9.0 ┆ 0.0 │ │ 2 ┆ B ┆ 0.0 ┆ 3.0 ┆ 0.166667 │ │ 2 ┆ B ┆ 1.0 ┆ 4.0 ┆ -0.333333 │ │ 2 ┆ B ┆ 2.0 ┆ 6.0 ┆ 0.166667 │ └────────┴────────┴─────┴─────┴─────────────┘   https://docs.pola.rs/user-guide/expressions/user-defined-python-functions/#combining-multiple-column-values
Question: I have a Shiny for Python app with some buttons. I want to change the color of a button. How can I do this? from shiny.express import ui with ui.layout_columns(): ui.input_action_button("red_button", "Make this button red!") ui.input_action_button("blue_button", "Make this button blue!") 

Answer: You can pass a style attribute and set the background-color: from shiny.express import ui with ui.layout_columns(): ui.input_action_button( "red_button", "Make this button red!", style = "background-color: red;" ) ui.input_action_button( "blue_button", "Make this button blue!", style = "background-color: lightblue;" )
Question: I have two same-architecture Linux boxes running identical versions of pypy, installed via pyenv. Consider one a "production" machine and one a "build" machine, such that the production machine cannot easily collect and configure all required build dependencies for modern source packages. I need to install a package on the production machine for which PyPI provides a source distribution but no pre-built wheel. python -m pip install example-package  works fine on the build machine, but some of the build dependencies are impractical to get/configure/make/install on the production machine. Is there a convenient method to migrate the python packages pip built from PyPI onto another computer?

Answer: I would do pretty much what @phd suggested in the comments. Instead of trying to install directly on your production machine, you can download and build everything on your build machine, then move the ready files over. Here’s how:  On your build machine, use pip to download the package and all its dependencies: python -m pip download example-package -d ./wheels  That grabs wheels and source archives into ./wheels.  If you see any .tar.gz files, build them into wheels so you have everything precompiled: python -m pip wheel --no-deps --wheel-dir=./wheels ./wheels/*.tar.gz   Copy the entire wheels folder to your production machine.  On your production machine, install from those local files without touching PyPI: python -m pip install --no-index --find-links=./wheels example-package    This way, your production machine unpacks prebuilt wheels—no compilation needed.
Question: I have a pandas dataframe of the following structure: d = {'I': ['A', 'B', 'C', 'D'], 'X': [ 1, 0, 3, 1], 'Y': [0, 1, 2, 1], 'Z': [1, 0, 0, 0], 'W': [3, 2, 0, 0]} df = pd.DataFrame(data=d, columns=['I','X', 'Y', 'Z', 'W']) df.set_index('I', inplace=True, drop=True)  I need to create a dict of dict to get data of all existing edges (indicated by nonzero values) between nodes: {'A': {'X': {1}, 'Z': {1}, 'W': {3}}, 'B': {'Y': {1}, 'W': {2}}, 'C': {'X': {3}, 'Y': {2}}, 'D': {'Y': {1}, 'X': {1}}}  I need it to create a network graph using Networkx library and perform some calculations on it. Obviously it would be possible to loop over every cell in the data frame to do this but my data is quite large and it would be inefficient. I'm looking for some better way possibly using vectorization and/or list comprehension. I've tried list comprehension but I'm stuck and cannot make it work. Can anyone suggest a more efficient way to do this please?

Answer: You can do this by combining df.iterrows() with a dictionary comprehension. Although iterrows() is not truly vectorized, it's still reasonably efficient for this kind of task and cleaner than using manual nested loops. For example, you could write: edge_dictionary = { node: {attribute: {weight} for attribute, weight in attributes.items() if weight != 0} for node, attributes in df.iterrows() }  If your DataFrame is very large and you’re concerned about performance, another approach is to first convert it into a plain dictionary of dictionaries using df.to_dict(orient='index') and then filter out the zeros. That would look like thiss: data_dictionary = df.to_dict(orient='index') edge_dictionary = { node: {attribute: {weight} for attribute, weight in connections.items() if weight != 0} for node, connections in data_dict.items() }