eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I want a label near the slider's handle, that displays the current value. I used the code provided on the forum and tried to adapt it to Python from PyQt5 import QtCore, QtGui, QtWidgets class test(QtWidgets.QWidget): def __init__(self, parent = None): super().__init__(parent) self.main_layout = QtWidgets.QHBoxLayout(self) self.main_layout.setContentsMargins(0, 0, 0, 0) self.main_layout.setSpacing(0) self.slider = QtWidgets.QSlider(QtCore.Qt.Vertical,self) self.slider.setMaximum(100) self.slider.setMinimum(0) self.slider.setTracking(True) self.label= QtWidgets.QLabel(self) self.label.setText("label") self.main_layout.addWidget(self.slider,0,QtCore.Qt.AlignLeft) self.main_layout.addWidget(self.label,0,QtCore.Qt.AlignBottom) self.updateLabel(0) self.setLayout(self.main_layout) self.slider.valueChanged.connect(self.updateLabel) def updateLabel(self,value): height= QtWidgets.QStyle.sliderPositionFromValue(0,100,self.slider.value(),self.slider.height()-self.label.height(), True) self.label.move(self.slider.width(), height) self.label.setText(str(value)) def resizeEvent(self,ev): self.updateLabel(self.slider.sliderPosition()) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) ui = test() ui.show() sys.exit(app.exec_())  As a result, I get a label with value, but it is always shown at the bottom of the window.  Once I resize my window, the label moves to the correct place.  Do I miss something?

Answer: With the help of @ekhumoro and @simon, I fixed the problem from PyQt5 import QtCore, QtGui, QtWidgets class test(QtWidgets.QWidget): def __init__(self, parent = None): super().__init__(parent) self.main_layout = QtWidgets.QHBoxLayout(self) self.main_layout.setContentsMargins(0, 0, 0, 0) self.main_layout.setSpacing(0) self.slider = QtWidgets.QSlider(QtCore.Qt.Vertical,self) self.slider.setMaximum(100) self.slider.setMinimum(0) self.slider.setTracking(True) self.label= QtWidgets.QLabel(self) self.label.setText("label") self.main_layout.addWidget(self.slider,0,QtCore.Qt.AlignLeft) self.updateLabel(0) self.setLayout(self.main_layout) self.slider.valueChanged.connect(self.updateLabel) def updateLabel(self,value): height= QtWidgets.QStyle.sliderPositionFromValue(0,100,self.slider.value(),self.slider.height()-self.label.height(), True) self.label.move(self.slider.width(), height) self.label.setText(str(value)) def resizeEvent(self,ev): self.updateLabel(self.slider.sliderPosition()) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) ui = test() ui.show() sys.exit(app.exec_())  The main difference is not adding the Qlabel inside of the layout, hence having more direct control over its location. One would need to account for label size while using the widget, since by default it's size shrinks to QSlider size
Question: How do you get a group_id column like this, grouping by columns col1 and col2 ?    col1 col2 group_id     A Z 1   A Y 2   A Z 1   B Z 3    based on such a DataFrame : df = pl.DataFrame({ 'col1': ['A', 'A', 'A', 'B'], 'col2': ['Z', 'Y', 'Z', 'Z']} )  In other words, I'm looking for polars equivalent to R data.table .GRP (df[, group_id:=.GRP, by = .(col1, col2)]). Thanks ! Context : I want to build an event ID because in my data, I have many detailed rows for one event. Once the event ID is created, I will use it to perform various window operations. I prefer to have this event ID rather than keeping a list of grouping variables.

Answer: If you don't care about "first occurrence has lower rank" then you can just rank combination of col1 and col2 columns.  pl.struct() to create one column out of col1, col2. pl.Expr.rank() to assign rank to rows.  df.with_columns(group_id = pl.struct("col1","col2").rank("dense"))  shape: (4, 3) ┌──────┬──────┬──────────┐ │ col1 ┆ col2 ┆ group_id │ │ --- ┆ --- ┆ --- │ │ str ┆ str ┆ u32 │ ╞══════╪══════╪══════════╡ │ A ┆ Z ┆ 2 │ │ A ┆ Y ┆ 1 │ │ A ┆ Z ┆ 2 │ │ B ┆ Z ┆ 3 │ └──────┴──────┴──────────┘  Otherwise, it will be a bit more complicated - you can create index to track current row order and then assign rank based on this index.  pl.DataFrame.with_row_index() to create an index / row number. pl.Expr.min() and pl.Expr.over() to get minimum index within col, col2. pl.Expr.rank() to "compress" this minimum to dense rank.  ( df.with_row_index("group_id") .with_columns( pl.col.group_id.min().over("col1","col2").rank("dense") ) )  shape: (4, 3) ┌──────────┬──────┬──────┐ │ group_id ┆ col1 ┆ col2 │ │ --- ┆ --- ┆ --- │ │ u32 ┆ str ┆ str │ ╞══════════╪══════╪══════╡ │ 1 ┆ A ┆ Z │ │ 2 ┆ A ┆ Y │ │ 1 ┆ A ┆ Z │ │ 3 ┆ B ┆ Z │ └──────────┴──────┴──────┘
Question: I'm trying to order this dataframe in quarterly order using the list sortTo as reference to put it into a table. import pandas as pd # Sample DataFrame data = {'QuarterYear': ["Q1 2024", "Q2 2024", "Q3 2023", 'Q3 2024', "Q4 2023", "Q4 2024"], 'data1': [5, 6, 2, 1, 10, 3], 'data2': [12, 4, 2, 7, 2, 9]} sortTo = ["Q3 2023", "Q4 2023", "Q1 2024", 'Q2 2024', "Q3 2024", "Q4 2024"] df = pd.DataFrame(data) df.reindex(sortTo)  I've tried re-index, sort_values to no avail. I cannot use np.sort as the quarters are not numerical. Current output:  QuarterYear data1 data2 0 Q1 2024 5 12 1 Q2 2024 6 4 2 Q3 2023 2 2 3 Q3 2024 1 7 4 Q4 2023 10 2 5 Q4 2024 3 9 

Answer: Code use key parameter for custom sorting. out = df.sort_values( 'QuarterYear', key=lambda x: x.map({k: n for n, k in enumerate(sortTo)}) )  out:  QuarterYear data1 data2 2 Q3 2023 2 2 4 Q4 2023 10 2 0 Q1 2024 5 12 1 Q2 2024 6 4 3 Q3 2024 1 7 5 Q4 2024 3 9   If your data consists of years and quarters, the following code should do the sorting you need without sortTo. out = df.sort_values( 'QuarterYear', key=lambda x: x.str.replace(r'(Q\d) (\d+)', r'\2 \1', regex=True) )
Question: I have the following problem: I want to save a figure with a specific width, but auto-determine its height. Let's look at an example: import matplotlib.pyplot as plt import numpy as np fig,ax=plt.subplots(figsize=(5,5),layout='constrained') x=np.linspace(0,2*np.pi) y=np.sin(x) ax.set_aspect('equal') ax.plot(x,y) plt.show() fig.savefig('test.pdf',format='pdf')  Here, I want the figure to be 5 inches wide, I want the axis to use up all the horizontal space, but I don't really care about the exact vertical size. Basically exactly, what plt.show() gives me:  fig.savefig() gives me a lot of whitespace on top and below the figure (obviously, because I have defined figsize=(5,5)). Using the option bbox_inches='tight' almost does what I want to, but it re-sizes the x-direction of the figure (in this case to roughly 5.1 inches). I have thus not found any way of saving this figure with a width of exactly 5 inches but an auto-determined height. The only way I can achieve what I want is to manually decrease the figsize until I see that the figure starts shrinking in x-direction.

Answer: Thanks to RuthC for providing the answer in a comment, the following seems to solve my problem: fig.savefig('test.pdf', format='pdf',bbox_inches='tight', pad_inches='layout')  https://matplotlib.org/stable/users/prev_whats_new/whats_new_3.8.0.html#pad-inches-layout-for-savefig
Question: Doing a project in university data science course and I'm working with API-s for the first time. I need to fetch different data about tracks using Spotify API, but I have encountered a problem early on. I can get access to some basic data about tracks popularity, duration, etc but I get 403 error when trying to fetch audio-features aswell. As far as I have searched for help on the web, apparently I have insufficient permissions in the scope of my Spotify access token. But I don't know how to get this problem fixed. I attempted to fetch audio features (e.g., tempo, danceability, energy) for the same tracks using sp.audio_features(track_ids). This is where the issue arises. When I run the code, I receive an error, and the audio features are not retrieved.  Error: "HTTP Error for GET to https://api.spotify.com... with Params: {} returned 403 due to None"  from spotipy.oauth2 import SpotifyOAuth import spotipy from dotenv import load_dotenv import os import pandas as pd load_dotenv() client_id = os.getenv("SPOTIPY_CLIENT_ID") client_secret = os.getenv("SPOTIPY_CLIENT_SECRET") redirect_uri = os.getenv("SPOTIPY_REDIRECT_URI") auth_manager = SpotifyOAuth( client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri, scope="user-top-read" ) sp = spotipy.Spotify(auth_manager=auth_manager) # Fetch data top_tracks = sp.current_user_top_tracks(limit=10) track_data = [] track_ids = [] for item in top_tracks['items']: track_ids.append(item['id']) track_data.append({ 'track_id': item['id'], 'track_name': item['name'], 'artists': ", ".join(artist['name'] for artist in item['artists']), 'popularity': item['popularity'], 'explicit': item['explicit'], 'duration_ms': item['duration_ms'] }) # Fetch audio features try: audio_features = sp.audio_features(track_ids) # Causing errors audio_features_df = pd.DataFrame(audio_features) df = pd.DataFrame(track_data) df = pd.merge(df, audio_features_df, left_on='track_id', right_on='id') except: print("Can not access audio features") df = pd.DataFrame(track_data) df.head(10) 

Answer: Spotify deprecated several API endpoints on November 27th 2024. The get-audio-features was one of those endpoints. This in turn affected spotipy as well.
Question: I want to declare the type of a variable which I do not declare myself, but which I know exists. Motivation I am currently working with the kivy library, which does a poor job indicating to static type checkers what types its fields have. I would like to indicate the types myself so I can get autocompletions. In the example below, self.ids will be a DictProperty (I believe this is a subclass of a dict) and I want to declare the type of self.ids.task_list_area. Problem statement How do I declare the type of self.ids.task_list_area in the following code? class MyScreenManager(ScreenManager): task_list = ["Do Homework", "Take out Trash"] class DetailsScreen(Screen): manager: MyScreenManager # this works # ids: dict # ids.task_list_area: BoxLayout # this does not work because ids does not exist def display_tasks(self): print(self.manager.task_list) # this is correctly infered as list[str] # this works at runtime, but the type checker has no clue what is happening print(self.ids.task_list_area) def display_tasks_with_annotation(self): self.ids.task_list_area: BoxLayout # this does not error, but does not work either print(self.ids.task_list_area) # type checker still knows nothing 

Answer: You should annotate ids with a type for which it is known that it has an attribute task_list_area of type BoxLayout. If kivy doesn't provide such a type, you can create one using typing.Protocol: from typing import Protocol class HasTaskListArea(Protocol): task_list_area: BoxLayout class DetailsScreen(Screen): ids: HasTaskListArea # ...
Question: I'm looking to get the UUID of the entries of a group with pykeepass. #!/usr/bin/env python3 from pykeepass import PyKeePass kp = PyKeePass('dbtest.kdbx', password='password') kpGroup = kp.find_groups(name='GroupTest', first=True) print(kpGroup.entries)  The code will return this : [Entry: "GroupTest/test (test@test.com)", Entry: "GroupTest/test (test@test.com)"]  The problem is when 2 entries of the group have a similar Title (test) and username (test@test.com), I am unable to use kp.find_entries() on either the Title or Username, having the uuid displayed by kpGroup.entries for each of those entries will solve the problem. I would like to have something like this displayed : [Entry: "GroupTest/test (test@test.com) UUID=uuid", Entry: "GroupTest/test (test@test.com) UUID=uuid"] 

Answer: It is the difference between  the real content of kpGroup.entries, the brief info which you get with the print(kpGroup.entries).  So you may obtain arbitrary other info from this list of group entries. You wanted this: [(e.group, e.title, e.uuid) for e in kpGroup.entries]  You will obtain something as this:  [(Group: "GroupTest", 'test', UUID('dfc7622c-1cc1-f045-84fd-6e6acab78539')), (Group: "GroupTest", 'test', UUID('d14a2296-703e-2a40-aa78-f559f6ec090f'))]
Question: I am trying to implement a softmax function that takes in signed int8 input and returns a signed int8 output array. The current implementation I have going is this,  import numpy as np def softmax_int8(inputs): inputs = np.array(inputs, dtype=np.int8) x = inputs.astype(np.int32) x_max = np.max(x) x_shifted = x - x_max scale_factor = 2 ** 14 exp_limit = 16 exp_x = np.clip(x_shifted + exp_limit, 0, None) exp_x = (1 << exp_x) sum_exp_x = np.sum(exp_x) if sum_exp_x == 0: sum_exp_x = 1 softmax_probs = (exp_x * scale_factor) // sum_exp_x max_prob = np.max(softmax_probs) min_prob = np.min(softmax_probs) range_prob = max_prob - min_prob if max_prob != min_prob else 1 scaled_probs = ((softmax_probs - min_prob) * 255) // range_prob - 128 outputs = scaled_probs.astype(np.int8) return outputs  I test it using this input, Input = [101, 49, 6, -34, -75, -79, -38, 120, -55, 115] but I get this output array([-128, -128, -128, -128, -128, -128, -128, 127, -128, -121],dtype=int8). My expected output is array([-57, -70, -79, -86, -92, -94, -88, -54, -91, -56], dtype=int8). What am I doing wrong here and how can I fix it?

Answer: I think there are different mathematical definitions of softmax in different contexts.  Wikipedia definition (on real numbers): exp(z) / sum(exp(z)) What I inferred from your code: (1<<(z-z_max + 16)) / sum((1 << (z-z_max + 16))) or something similar. 1<< === 2** obviously.  The major difference is the base number of the exponential. With base too high you are highly likely to get underflow and get a lot of -128. Besides there are also a biase that maps the result to [-128, 127] range, which is trival and less important It's highly likely that the library that you takes test cases from use a different definition than both of above. I did some testing with your test case and floating point definition of softmax with matplotlib, and the following expression gives a good fit: softmax_naive = (np.exp(inarr / 128) / np.sum(np.exp(inarr / 128)) * 256) - 100  You can imagine that you probably need to do a >>7 to input bytes before doing 1<< 2-based exponential. To give completely identical result, surely you should dig into that library code, which I didn't have time to do. Below are validation codes: import numpy as np import matplotlib.pyplot as plt inarr = np.array([101, 49, 6, -34, -75, -79, -38, 120, -55, 115], dtype=np.int8).astype(np.double) expected_arr = np.array([-57, -70, -79, -86, -92, -94, -88, -54, -91, -56], dtype=np.int8).astype(np.double) print(expected_arr) softmax_naive = (np.exp(inarr / 128) / np.sum(np.exp(inarr / 128)) * 256) - 100 print(softmax_naive - expected_arr) plt.plot(inarr) plt.plot(expected_arr) plt.plot(softmax_naive) plt.show()
Question: Let's say we want to create/maintain a dict with below key structure { "a": {"bb": {"01": "some value 01", "02": "some value 02", } }, }  We use dpath .new() as below to do this import dpath d=dict() ; print(d) # d={} dpath.new(d,'a/bb/00/c', '00val') ; print(d) # d={'a': {'bb': [{'c': '00val'}]}} # d # NOTE this will set :bb as a list, and set at bb[00] ie idx=0 of list bb # d # what we want instead is d={'a': {'bb': {'00': {'c': '00val'} }}} # d # what we want is NOT d={'a': {'bb': [ {'c': '00val'} ]}}  So when using 00 in the path, dpath translate it into list index at 0 instead of dict key 00 The question is how to set key as 00? Currently I have to dodge it by setting prefix s ie s00 s01 s02

Answer: I traced into the source, and noticed that it calls a Creator, documented in the function as:  creator allows you to pass in a creator method that is responsible for creating missing keys at arbitrary levels of the path (see the help for dpath.path.set)  I copied _default_creator from dpath.segments, and just eliminated the code that treats int values as a list index. Here's the quick and dirty version: import dpath from dpath.segments import extend from typing import Sequence, MutableSequence def my_creator(current, segments, i, hints): segment = segments[i] length = len(segments) if isinstance(current, Sequence): segment = int(segment) if isinstance(current, MutableSequence): extend(current, segment) # Infer the type from the hints provided. if i < len(hints): current[segment] = hints[i][1]() else: # deleted the array stuff here current[segment] = {} d = dict() dpath.new(d, r'a/bb/00/c', '00val', creator=my_creator) print(d)  output  {'a': {'bb': {'00': {'c': '00val'}}}}
Question: In Python Polars I am trying to "group" two lists together, for example in a Pl.Struct, removing the group when one of the elements is Null. For example, given this DataFrame: df = pl.DataFrame({ "Movie": [[None, "IT",]], "Count": [[30, 27]], }) shape: (1, 2) ┌──────────────┬───────────┐ │ Movie ┆ Count │ │ --- ┆ --- │ │ list[str] ┆ list[i64] │ ╞══════════════╪═══════════╡ │ [null, "IT"] ┆ [30, 27] │ └──────────────┴───────────┘  I would like to get the result below:  shape: (1, 1) ┌───────────┐ │ data │ │ --- │ │ struct[2] │ ╞═══════════╡ │ {"IT",27} │ └───────────┘  However, the challenge I am finding in this case is that I need to do it in a single context (i.e. in a single with_columns). Is it possible?

Answer: df.with_columns( pl.struct( pl.col("Movie", "Count").list.gather( pl.col("Movie").list.eval(pl.element().is_not_null().arg_true()) ) .list.first() ) .alias("data") )  shape: (1, 3) ┌──────────────┬───────────┬───────────┐ │ Movie ┆ Count ┆ data │ │ --- ┆ --- ┆ --- │ │ list[str] ┆ list[i64] ┆ struct[2] │ ╞══════════════╪═══════════╪═══════════╡ │ [null, "IT"] ┆ [30, 27] ┆ {"IT",27} │ └──────────────┴───────────┴───────────┘
Question: I have written an iterative plot code for python using plt.draw(). While it works fine for python interpreter and ipython also, but it does not work in jupyter notebook. I am running python in virtualenv in Debian. The codes are import matplotlib.pyplot as plt import numpy as np N=5 x = np.arange(-3, 3, 0.01) fig = plt.figure() ax = fig.add_subplot(111) for n in range(N): y = np.sin(np.pi*x*n) line, = ax.plot(x, y) plt.draw() plt.pause(0.5) line.remove()  This works fine in command line such as  $ python a.py  But, in jupyter notebook, it plots first figure and then, repeat <Figure size ... 0 Axes> for N-1 times, not drawing the new figure inside axis. It looks the problem of jupyter, because no problem in running python code. How can I fix this? Or plotly would work in jupyter? (N.B. I happen to find a jupyter community site and posted there also.) 

Answer: Jupyter Notebook handles plot display differently. If you’re able to modify the code, you can try the following approach: %matplotlib widget import matplotlib.pyplot as plt import numpy as np from matplotlib.animation import FuncAnimation # Parameters for the plot N = 5 x = np.arange(-3, 3, 0.01) # Create a figure and axes fig, ax = plt.subplots() line, = ax.plot([], [], lw=2) # Line object # Set the limits for the axes ax.set_xlim(-3, 3) ax.set_ylim(-1.5, 1.5) # Initialization function: called at the start def init(): line.set_data([], []) return line, # Update function: called for each frame def update(n): y = np.sin(np.pi * x * n) # Calculate sine wave line.set_data(x, y) # Update line data return line, # Create the animation using FuncAnimation ani = FuncAnimation(fig, update, frames=N, init_func=init, blit=True, interval=500)  This method works well for me in VSCode. If you want more control over the animation, you can display it directly in the Jupyter Notebook using: # Display the animation in the Jupyter notebook from IPython.display import HTML HTML(ani.to_jshtml()) # Render the animation as HTML/JS   The frames parameter specifies the data passed to the update(n) function during each interaction step. Setting frames=N is equivalent to using frames=range(N). This operates similarly to the following loop: for n in range(N): update(n)  For more details about FuncAnimation, please refer to the documentation at: https://matplotlib.org/stable/api/_as_gen/matplotlib.animation.FuncAnimation.html.
Question: I was given a file to practice pandas on, and was asked this question: Q: Access the last 10 rows and the first two columns of the index dataframe. So, I tried this code: df = index[(index.tail(10)) & (index.iloc[:, :2])] df  but it gave me an error. How do I access the last 10 rows and first two columns of a dataframe? I expected to have the last 10 rows of the first two columns in a variable.

Answer: One way is to try with .iloc df = index.iloc[-10: , :2]   -10: takes last 10 rows. Note the negative and look more into how iloc (section Indexing both axes). :2 after comma takes first two column i.e. 0 and 1.
Question: I'm trying to figure out the most elegant way in Polars to find the two bracketing rows (first above and first below) a specific target. Essentially the Min > 0 & the Max < 0. data = { "strike": [5,10,15,20,25,30], "target": [16] * 6, } df = (pl.DataFrame(data) .with_columns( diff = pl.col('strike') - pl.col('target'))) shape: (6, 3) ┌────────┬────────┬──────┐ │ strike ┆ target ┆ diff │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ i64 │ ╞════════╪════════╪══════╡ │ 5 ┆ 16 ┆ -11 │ │ 10 ┆ 16 ┆ -6 │ │ 15 ┆ 16 ┆ -1 │ │ 20 ┆ 16 ┆ 4 │ │ 25 ┆ 16 ┆ 9 │ │ 30 ┆ 16 ┆ 14 │ └────────┴────────┴──────┘  This is what I'm trying to arrive at. shape: (2, 3) ┌────────┬────────┬──────┐ │ strike ┆ target ┆ diff │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ i64 │ ╞════════╪════════╪══════╡ │ 15 ┆ 16 ┆ -1 │ │ 20 ┆ 16 ┆ 4 │ └────────┴────────┴──────┘  I am able to do it through two separate filter operations but I can't seem to string these together and would like to avoid having to vstack the two individual results back together into a single data frame if possible. df1 = (pl.DataFrame(data) .with_columns( diff = pl.col('strike') - pl.col('target')) .filter((pl.col('diff') > 0)).min()) shape: (1, 3) ┌────────┬────────┬──────┐ │ strike ┆ target ┆ diff │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ i64 │ ╞════════╪════════╪══════╡ │ 20 ┆ 16 ┆ 4 │ └────────┴────────┴──────┘ df2 = (pl.DataFrame(data) .with_columns( diff = pl.col('strike') - pl.col('target')) .filter((pl.col('diff') < 0)).max()) shape: (1, 3) ┌────────┬────────┬──────┐ │ strike ┆ target ┆ diff │ │ --- ┆ --- ┆ --- │ │ i64 ┆ i64 ┆ i64 │ ╞════════╪════════╪══════╡ │ 15 ┆ 16 ┆ -1 │ └────────┴────────┴──────┘ 

Answer: data = {"strike": [5,10,15,20,25,30]} target = 16 diff = pl.col("strike") - target ( pl.DataFrame(data) # diff and target can be added as columns, but this is not actually needed # .with_columns(target=target, diff=diff) # filter for where the diff equals the min above 0, or the max below 0 .filter( (diff == diff.filter(diff > 0).min()) | (diff == diff.filter(diff < 0).max()) ) ) # shape: (2, 1) # ┌────────┐ # │ strike │ # │ --- │ # │ i64 │ # ╞════════╡ # │ 15 │ # │ 20 │ # └────────┘