eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: In attempting to put together a very simple example to illustrate an issue I'm having with pyparsing, I have found that I can't get my simple example to work - this example is barely more complex than Hello, World! Here is my example code: import pyparsing as pp import textwrap as tw text = ( """ A) Red B) Green C) Blue """ ) a = pp.AtLineStart("A)") + pp.Word(pp.alphas) b = pp.AtLineStart("B)") + pp.Word(pp.alphas) c = pp.AtLineStart("C)") + pp.Word(pp.alphas) grammar = a + b + c grammar.run_tests(tw.dedent(text).strip())  I would expect it to return ["A)", "Red", "B)", "Green", "C)", "Blue"] but instead I get: A) Red A) Red ^ ParseException: not found at line start, found end of text (at char 6), (line:1, col:7) FAIL: not found at line start, found end of text (at char 6), (line:1, col:7) B) Green B) Green ^ ParseException: Expected 'A)', found 'B' (at char 0), (line:1, col:1) FAIL: Expected 'A)', found 'B' (at char 0), (line:1, col:1) C) Blue C) Blue ^ ParseException: Expected 'A)', found 'C' (at char 0), (line:1, col:1) FAIL: Expected 'A)', found 'C' (at char 0), (line:1, col:1)  Why would it say it's found end of text after the first line??? Why is it expecting A) after the first line??? (Note: textwrap.dedent() and strip() have no impact on the results of this script.)

Answer: My dude! You forgot to wrap raw string into a list! I tested for 30 mins and felt something was odd, then found this in document:  run_tests(tests: Union[str, List[str]], ...) -> ... Execute the parse expression on a series of test strings, showing each test, the parsed results or where the parse failed. Quick and easy way to run a parse expression against a list of sample strings. Parameters: tests - a list of separate test strings, or a multiline string of test strings  Basically you (and me for last half hour) by doing this: grammar.run_tests(tw.dedent(text).strip())  ...Was telling it to treat each line as individual tests! """ # You are a test 0 now! A) Red # You are a test 1 B) Green # test 2 for you, C) Blue # test 3 for ya, """ # finally you're test 4! Mhahahah!  (And of course the use of pp.line_end so line actually consume end of line) >>> import pyparsing as pp ... import textwrap as tw ... ... text = ( ... """ ... A) Red ... B) Green ... C) Blue ... """ ... ) ... ... a = pp.AtLineStart("A)") + pp.Word(pp.alphas) + pp.line_end.suppress() ... b = pp.AtLineStart("B)") + pp.Word(pp.alphas) + pp.line_end.suppress() ... c = pp.AtLineStart("C)") + pp.Word(pp.alphas) ... ... grammar = a + b + c ... grammar.run_tests([tw.dedent(text).strip()]) A) Red B) Green C) Blue ['A)', 'Red', 'B)', 'Green', 'C)', 'Blue'] >>>
Question: I have the following working in python 3.8.1 @unique class Encoder(IntFlag): # H1 sensor signal H1 = 0x00 # H2 sensor signal H2 = 0x01 # H3 sensor signal H3 = 0x02  Then I am trying to catch with an assert if the value is not within the enum, i.e. from enum import unique, IntFlag signal = Encoder.H1 assert signal in iter(Encoder), f"Valid Encoder line is integer 0 to 2 inclusive."  I noticed on python 3.8, the signal in iter(Encoder) returns True but False in python 3.12.4  It might be a change in some version from 3.8.1 to 3.12.4 but I am not sure where to star looking for getting this working in both.

Answer: enum.Flag implements __contains__. For membership checks, drop the iter call and just use: signal in Encoder  This will work in both 3.8.1 and 3.12.4. Note: The change in iteration behavior for flags happened in Python 3.11 and is mentioned in the changelog here. Also, you should probably be using IntEnum rather than IntFlag anyway. IntFlag would be appropriate in a use case where Encoder.H2 | Encoder.H3 was meaningful, and meant "both of these". In your use case, Encoder.H2 | Encoder.H3 is an invalid value.
Question: Any suggestions? If I ever the row height in openpyxl, delete_rows will appear to remove the rows, but when I save and open in Excel, the rows are empty but not all row information is removed. The scroll bar still scrolls to where the last row was prior to deleting. So "ghosts" of the empty rows remain. Sample code: from openpyxl import Workbook # Create a new workbook and select the active sheet wb = Workbook() ws = wb.active # Write values into the cells for row in range(1, 101): for col in range(1, 6): cell = ws.cell(row=row, column=col) cell.value = f"Cell {row}-{col}" # Set row height for row in ws.iter_rows(): ws.row_dimensions[row[0].row].height = 14.4 # Unset row height for row in ws.iter_rows(): ws.row_dimensions[row[0].row].height = None # Delete the rows ws.delete_rows(35, 100-35) # Save the workbook wb.save('test.xlsx')   If I comment out both row height loops, it works correctly. If either loop is included, remnants of the empty rows remain. 

Answer: Yes, the delete clears the cells however as the row dimensions have been adjusted (by changing the height) they now exist in the Sheet profile. The row delete function does not remove that from the Sheet. The result being that the workbook retains the rows to 100 defined in the Sheet with those above row 35 having no cell details. You can fix this by deleting the row dimensions too. for x in range(36, 101): del ws.row_dimensions[x]  Of course deleting the row dimension instead of setting to None would also affect the same change; # Unset row height for row in ws.iter_rows(): # ws.row_dimensions[row[0].row].height = None del ws.row_dimensions[row[0].row]
Question: I've set up a dataframe: df = pd.DataFrame({'Name': ['Alice', 'Bob', 'Aritra'], 'Age': [25, 30, 35], 'Location': ['Seattle', 'New York', 'Kona']}, index=([10, 20, 30]))  I ask the user to put in which row of the data they want to see. They input a 0, indicating the first row. However, df.loc[0] does not refer to the first row. Instead, it doesn't exist, because the index only has the values 10, 20, and 30. Is there terminology to distinguish these two types of indexes? The best I could come up with is ordinal indexes (for "What number row is this?") and dataframe indexes (for "What is the index of this row in the dataframe?"). To clarify, under my definitions, df.index(ordinal_index) == df_index. Is there a standard terminology for this?

Answer: If you look at the documentation you've linked, pandas uses the term index label to describe what you call a dataframe index. It's more clearly explained in this documentation on indexing, where the term position is used to refer to what you call an ordinal index. (Note that some popular answers on Stack Overflow use the term location rather than position.) Note that this terminology might get confusing because:  The df.index property is essentially a mapping from row positions to row labels. (And this issue is especially compounded since the positions and labels are usually equivalent for most dataframe rows.) The df.columns.get_loc(column_label) function returns the position of a column with the given column label (and yet is called get_loc, thus why some people use the term location instead of position). The df.loc property expects to be accessed with a label (not a position/location): e.g. df.loc[row_label, column_label]. It's the (now depreciated) df.iloc function that should be accessed with a position/location.  (I'm answering my own question because I found an answer just before I posted the question - I've also updated this answer as I've found more accurate info.)
Question: I have the following DataFrame and want to calulate the "share".  import pandas as pd d = {"col1":["A", "A", "A", "B", "B", "B"], "col2":["start_amount", "mid_amount", "end_amount", "start_amount", "mid_amount", "end_amount"], "amount":[0, 2, 8, 1, 2, 3]} df_test = pd.DataFrame(d) df_test["share"] = 0 for i in range(len(df_test)): df_test.loc[i, "share"] = df_test.loc[i, "amount"] / df_test.loc[(df_test["col1"] == df_test.loc[i, "col1"]) & (df_test["col2"] == "end_amount"), "amount"].values  This works but is far from efficient. Is there a better way to do my calculation?

Answer: This is equivalent to selecting the rows with "end_amount", then performing a map per "col1" to then divide "amount": s = df_test.loc[df_test['col2'].eq('end_amount')].set_index('col1')['amount'] df_test['share'] = df_test['amount']/df_test['col1'].map(s)  Output:  col1 col2 amount share 0 A start_amount 0 0.000000 1 A mid_amount 2 0.250000 2 A end_amount 8 1.000000 3 B start_amount 1 0.333333 4 B mid_amount 2 0.666667 5 B end_amount 3 1.000000
Question: I have been using altair for a time and this is the first time that I have experienced this issue, I have this simple code: import pandas as pd import altair as alt # extract data into a nested list data = { "Name of District": ["Kollam", "Beed", "Kalahandi", "West Medinipur", "Birbhum", "Howrah"], "No. of Cases": [19, 11, 42, 145, 199, 85], } # create a dataframe from the extracted data df = pd.DataFrame( data ) # Display the first 5 rows print(df.head().to_markdown(index=False, numalign="left", stralign="left")) # Create a bar chart with `Name of District` on the x-axis and `No. of Cases` on the y-axis chart = alt.Chart(df).mark_bar().encode( x='Name of District', y='No. of Cases', tooltip=['Name of District', 'No. of Cases'] ).properties( title='Bar Chart of No. of Cases by Name of District' ).interactive() # Save the chart chart.save('no_of_cases_by_name_of_district_bar_chart.json') display(chart)  it returns this plot:  If I plot it using matplotlib, then i get the correct plot:  I have tried this on colab and on my local machine and I have gotten the same results, why could this happen?

Answer: Altair requires that special characters be escaped in the column names. See documentation here. This code should produce the plot you're looking for. chart = alt.Chart(df).mark_bar( ).encode( x='Name of District', y='No\. of Cases', tooltip=['Name of District', 'No\. of Cases'] ).properties( title='Bar Chart of No. of Cases by Name of District' ).interactive()
Question: I'm trying to truncate floating point numbers in my DataFrame to a desired number of decimal places. I've found that this can be done using Pandas and NumPy here, but I've also seen that it might be possible with polars.Config.set_float_precision. Below is my current approach, but I think I might be taking extra steps. import polars as pl data = { "name": ["Alice", "Bob", "Charlie"], "grade": [90.23456, 80.98765, 85.12345], } df = pl.DataFrame(data) ( df # Convert to string .with_columns( pl.col("grade").map_elements( lambda x: f"{x:.5f}", return_dtype=pl.String ).alias("formatted_grade") ) # Slice to get desired decimals .with_columns( pl.col("formatted_grade").str.slice(0, length = 4) ) # Convert back to Float .with_columns( pl.col("formatted_grade").cast(pl.Float64) ) ) 

Answer: You can use the Polars - Numpy integration like this: df = df.with_columns(truncated_grade=np.trunc(pl.col("grade") * 10) / 10)  Output: ┌─────────┬──────────┬─────────────────┐ │ name ┆ grade ┆ truncated_grade │ │ --- ┆ --- ┆ --- │ │ str ┆ f64 ┆ f64 │ ╞═════════╪══════════╪═════════════════╡ │ Alice ┆ 90.23456 ┆ 90.2 │ │ Bob ┆ 80.98765 ┆ 80.9 │ │ Charlie ┆ 85.12345 ┆ 85.1 │ └─────────┴──────────┴─────────────────┘  Full code: import numpy as np import polars as pl data = { "name": ["Alice", "Bob", "Charlie"], "grade": [90.23456, 80.98765, 85.12345], } df = pl.DataFrame(data) df = df.with_columns(truncated_grade=np.trunc(pl.col("grade") * 10) / 10) print(df)
Question: Ubuntu 20.04 Python 3.8 Got error  ImportError: cannot import name 'windowed_complete' from 'more_itertools'  However more_itertools is clearly installed. Is it possible, that Python 3.9 required to run windowed_complete ? Or how this error should be resolved ? Traceback (most recent call last): File "diarize.py", line 9, in <module> from nemo.collections.asr.models.msdd_models import NeuralDiarizer File "/home/admin/.local/lib/python3.8/site-packages/nemo/collections/asr/__init__.py", line 15, in <module> from nemo.collections.asr import data, losses, models, modules File "/home/admin/.local/lib/python3.8/site-packages/nemo/collections/asr/losses/__init__.py", line 16, in <module> from nemo.collections.asr.losses.audio_losses import SDRLoss File "/home/admin/.local/lib/python3.8/site-packages/nemo/collections/asr/losses/audio_losses.py", line 21, in <module> from nemo.collections.asr.parts.preprocessing.features import make_seq_mask_like File "/home/admin/.local/lib/python3.8/site-packages/nemo/collections/asr/parts/preprocessing/__init__.py", line 16, in <module> from nemo.collections.asr.parts.preprocessing.features import FeaturizerFactory, FilterbankFeatures, WaveformFeaturizer File "/home/admin/.local/lib/python3.8/site-packages/nemo/collections/asr/parts/preprocessing/features.py", line 44, in <module> from nemo.collections.asr.parts.preprocessing.perturb import AudioAugmentor File "/home/admin/.local/lib/python3.8/site-packages/nemo/collections/asr/parts/preprocessing/perturb.py", line 50, in <module> from nemo.collections.common.parts.preprocessing import collections, parsers File "/home/admin/.local/lib/python3.8/site-packages/nemo/collections/common/parts/preprocessing/collections.py", line 23, in <module> from nemo.collections.common.parts.preprocessing import manifest, parsers File "/home/admin/.local/lib/python3.8/site-packages/nemo/collections/common/parts/preprocessing/parsers.py", line 23, in <module> from nemo.collections.common.parts.preprocessing import cleaners File "/home/admin/.local/lib/python3.8/site-packages/nemo/collections/common/parts/preprocessing/cleaners.py", line 17, in <module> import inflect File "/home/admin/.local/lib/python3.8/site-packages/inflect/__init__.py", line 80, in <module> from more_itertools import windowed_complete ImportError: cannot import name 'windowed_complete' from 'more_itertools' (/usr/lib/python3/dist-packages/more_itertools/__init__.py)``` 

Answer: pip install more-itertools==10.3.0 (latest version) solved it
Question: I'm working on customizing Odoo and have encountered an issue with the sale_line_id field. I'm inheriting the project.task model in my custom module (wsl_available_drivers). In view, I need to make the sale_line_id field invisible. <record id="view_task_form2" model="ir.ui.view"> <field name="name">Project.task.view.form.inherit.available.drivers</field> <field name="model">project.task</field> <field name="inherit_id" ref="project.view_task_form2"/> <field name="arch" type="xml"> <xpath expr="//field[@name='user_ids']" position="before"> <field name="first_user" invisible="1"/> <field name="Journey_start_date"/> <field name="recurring_task" invisible="1"/> </xpath> <xpath expr="//field[@name='sale_line_id']" position="attribute"> <attribute name="invisible">1</attribute> </xpath> </field> </record>  When attempting to hide sale_line_id using XPath, I receive an error stating that the field does not exist in the project.task model. Upon further investigation: I found that sale_line_id is related to the Task model, with relatedmodel pointing to sale.order.line. However, I cannot locate the sale_line_id field within the sale.order.line model or its related modules.

Answer: The sale_project module adds two sale_line_id fields and the visibility of the field depends on the group to which the user belongs, the second field has an additional invisible attribute The XPath expression will match the first node it finds. If you need to hide the two fields, add another XPath to target the second field Example: <record id="view_task_form_inherit" model="ir.ui.view"> <field name="name">project.task.form.inherit</field> <field name="model">project.task</field> <field name="inherit_id" ref="sale_project.view_sale_project_inherit_form"/> <field name="arch" type="xml"> <xpath expr="//field[@name='sale_line_id']" position="attributes"> <attribute name="invisible">1</attribute> </xpath> <xpath expr="//field[@name='sale_line_id'][2]" position="attributes"> <attribute name="invisible">1</attribute> </xpath> </field> </record>  For more details check the View resolution and Inheritance specs documentation sections
Question: For each group in a groupby, I want to sum certain rows from several columns and output them in a new column, is_m_days.  Each Group (a Group has CT/RT and has a Quantity from 1 or 2 or 3 or more rows, randomly mixed up) in 'ATEXT' For the Sum, each group has a Row before and after.  DataFrame: data = {'ATEXT': ['', 'CT', 'RT', '', '', '', '', 'CT', 'CT', 'CT', 'TT', ''], 'BEGUZ_UE': [11.0, 23.0, 33.0, 15.0, 12.75, 19.75, 14.75, 23.0, 24.0, 24.0, 33.0, 15.0], 'subtract': [0.0, 0.0, 0.0, 0.2, np.nan, np.nan, 2.0, np.nan, np.nan, np.nan, np.nan, 0.0], 'add': [3.92, 0.0, 0.0, 0.0, np.nan, np.nan, 0.0, np.nan, np.nan, np.nan, np.nan, 3.57], 'UE_more_days': [np.nan, np.nan, 56.0, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, 104.0, np.nan]}  Result should be:  ATEXT BEGUZ_UE subtract add UE_more_days is_m_days 0 11.00 *0.00* *3.92* 1 CT *23.00* 0.00 0.00 2 RT *33.00* 0.00 0.00 56.0 3 *15.00* 0.20 0.00 *74.92* 4 12.75 5 19.75 6 14.75 *2.00* *0.00* 7 CT *23.00* 8 RT *24.00* 9 CT *24.00* 10 CT *33.00* 104.0 11 *15.00* 0.00 3.57 *117.00* 12 etc  My try was: m = df['ATEXT'].eq("") cond = (~m) & m.shift(-1) df['UE_more_days'] = (df['BEGUZ_UE'].mask(m) .groupby(m.cumsum()).cumsum() .where(cond) ) tmv = (df[['subtract', 'add']] .shift() .groupby(m.cumsum()) .transform('max') .eval('add-subtract') ) df['is_m_days'] = (df.groupby(m[::-1].cumsum())['BEGUZ_UE'] .transform('sum') .add(tmv) .where(cond) .shift() )  Is there a better solution?

Answer: Your approach is good, you could simplify it to use a single groupby (with extra boolean masks): m1 = df['ATEXT'].eq('') m2 = m1 & m1.shift(fill_value=True) m3 = m1!=m2 group = m2.cumsum() df.loc[m3, 'is_m_days'] = (pd .DataFrame({'A': df['BEGUZ_UE'].mask(m2), 'B': df['add'].sub(df['subtract']).where(m2)}) .groupby(group).transform('sum').sum(axis=1) )  Output:  ATEXT BEGUZ_UE subtract add UE_more_days is_m_days 0 11.00 0.0 3.92 NaN NaN 1 CT 23.00 0.0 0.00 NaN NaN 2 RT 33.00 0.0 0.00 56.0 NaN 3 15.00 0.2 0.00 NaN 74.92 4 12.75 NaN NaN NaN NaN 5 19.75 NaN NaN NaN NaN 6 14.75 2.0 0.00 NaN NaN 7 CT 23.00 NaN NaN NaN NaN 8 CT 24.00 NaN NaN NaN NaN 9 CT 24.00 NaN NaN NaN NaN 10 TT 33.00 NaN NaN 104.0 NaN 11 15.00 0.0 3.57 NaN 117.00
Question: I am creating a Gtk file chooser dialog as follows (see below for full example): dialog = Gtk.FileChooserDialog( title="Select a File", action=Gtk.FileChooserAction.OPEN)  I would also like to add a checkbox and dropdown combobox as extra widgets. Adding one extra widget works fine: cb = Gtk.CheckButton("Only media files") dialog.set_extra_widget(cb)  However, I would like to have a label and a combo box as well. I tried this: cb = Gtk.CheckButton("Only media files") dialog.set_extra_widget(cb) db = Gtk.ComboBoxText() db.append_text("Option 1") db.append_text("Option 2") db.set_active(0) dialog.set_extra_widget(db)  However this only shows the combo box, not the check button. I thought maybe only one widget is allowed, so I created an hbox: cb = Gtk.CheckButton("Only media files") db = Gtk.ComboBoxText() db.append_text("Option 1") db.append_text("Option 2") db.set_active(0) hbox = Gtk.HBox(spacing=10) hbox.pack_start(cb, False, False, 0) hbox.pack_start(db, False, False, 0) dialog.set_extra_widget(hbox)  Nope, nothing is shown. That doesn't work either. Then I read in the manual that "To pack widgets into a custom dialog, you should pack them into the Gtk.Box, available via Gtk.Dialog.get_content_area()." So I tried this:  cb = Gtk.CheckButton("Only media files") db = Gtk.ComboBoxText() db.append_text("Option 1") db.append_text("Option 2") db.set_active(0) hbox = Gtk.HBox(spacing=10) hbox.pack_start(cb, False, False, 0) hbox.pack_start(db, False, False, 0) dbox = dialog.get_content_area() dbox.pack_start(hbox, False, False, 0)  Thus, my question is this: how can I add multiple custom widgets to the standard file chooser dialog from pygtk? Here is a minimal reproducible (I hope) example. Just exchange the code between scisors with the fragments above if you want to test it. import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk def on_button_clicked(button): dialog = Gtk.FileChooserDialog( title="Select a File", action=Gtk.FileChooserAction.OPEN) # 8< ------------------- cb = Gtk.CheckButton("Only media files") db = Gtk.ComboBoxText() db.append_text("Option 1") db.append_text("Option 2") db.set_active(0) hbox = Gtk.HBox(spacing=10) hbox.pack_start(cb, False, False, 0) hbox.pack_start(db, False, False, 0) dbox = dialog.get_content_area() dbox.pack_start(hbox, False, False, 0) # 8< ------------------- response = dialog.run() dialog.destroy() window = Gtk.Window() window.set_default_size(300, 100) window.connect("destroy", Gtk.main_quit) button = Gtk.Button(label="Open FileChooserDialog") button.connect("clicked", on_button_clicked) window.add(button) window.show_all() Gtk.main() 

Answer: It appears that solution #2 was the right way to go. However, I did not use the show_all method to actually show the widgets, which was the problem. With the following fragment the file chooser dialog with extra widgets works: cb = Gtk.CheckButton("Only media files") db = Gtk.ComboBoxText() db.append_text("Option 1") db.append_text("Option 2") db.set_active(0) hbox = Gtk.VBox(spacing=10) hbox.pack_start(cb, False, False, 0) hbox.pack_start(db, False, False, 0) hbox.show_all() dialog.set_extra_widget(hbox)
Question: I want to use polars to take a csv input and get for each row another column (e.g called json_per_row) where the entry per row is the json representation of the entire row. I also want to select only a subset of the columns to be included alongside the json_per_row column. Ideally I don’t want to hardcode the number / names of the columns of my input but just to illustrate I’ve provided a simple example below: # Input: csv with columns time, var1, var2,... s1 = pl.Series("time", [100, 200, 300]) s2 = pl.Series("var1", [1,2,3]) s3 = pl.Series("var2", [4,5,6]) # I want to add this column with polars somehow output_col = pl.Series("json_per_row", [ json.dumps({ "time": 100, "var1":1, "var2":4 }), json.dumps({ "time": 200, "var1":2, "var2":5 }), json.dumps({ "time":300 , "var1":3, "var2":6 }) ]) # Desired output df = pl.DataFrame([s1, output_col]) print(df)  So is there a way to do this with the functions in the polars library? I'd rather not use json.dumps if it's not needed since as the docs say it can affect performance if you have to bring in external / user defined functions. Thanks

Answer:  you can use read_csv() to read your csv data, but here I'll just use Series data you provided. .struct() to combine all the columns into one struct column. struct.json_encode() to convert to json.  ( pl.DataFrame([s1,s2,s3]) .select( pl.col.time, json_per_row = pl.struct(pl.all()).struct.json_encode() ) ) ┌──────┬────────────────────────────────┐ │ time ┆ json_per_row │ │ --- ┆ --- │ │ i64 ┆ str │ ╞══════╪════════════════════════════════╡ │ 100 ┆ {"time":100,"var1":1,"var2":4} │ │ 200 ┆ {"time":200,"var1":2,"var2":5} │ │ 300 ┆ {"time":300,"var1":3,"var2":6} │ └──────┴────────────────────────────────┘