eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: On Ubuntu or other Linux-based systems, Python 3.14's venv creates an extra executable named 𝜋thon: $ python --version Python 3.13.0 $ python -m venv .venv $ cd .venv/bin && ls Activate.ps1 activate activate.csh activate.fish pip pip3 pip3.13 python python3 python3.13  $ python --version Python 3.14.0a1+ $ python -m venv .venv $ cd .venv/bin && ls 𝜋thon Activate.ps1 activate activate.csh activate.fish pip pip3 pip3.14 python python3 python3.14  What does it do and why is it there?

Answer: This is an easter egg. The 𝜋thon executable works exactly the same as python, python3 and python3.14. The name 𝜋thon itself is a pun on "Python" and 𝜋 ("Pi") the mathematical constant, whose decimal representation starts with "3.14". This executable was originally named python𝜋 as a parallelism to python3.14 and other python3.xx executables that are only created on non-Windows operating systems. python𝜋 didn't make the cut, however, since people seem to like 𝜋thon more.
Question: I am need to merge 2 arrays together so if a = []  and b is array([76522, 82096], dtype=int64)  the merge will be [76522, 82096] but i am getting this in a form of decimal array([76522., 82096.])  here is my code a = np.concatenate((a, b))  how can i merge both arrays with same datatype?

Answer: Since a is empty, when it gets converted to a numpy array, it chooses a default dtype=float64. Do the conversion explicitly so you can specify the dtype. np.concatenate((np.array(a, dtype=np.int64), b))
Question: I want to produce 2 random 3x4 matrices where the entries are normally distributed, A and B. After that, I have a 2x2 matrix C = [[a,b][c,d]], and I would like to use it to produce 2 new 3x4 matrices A' and B', where A' = a A + b B, B' = c A + d B. In order to produce the matrices A and B, I was thinking to use this line of code: Z = np.random.normal(0.0, 1.0, [2,3, 4])  But, given the matrix C, I don't know how to use simple Numpy vectorization to achieve the matrices A' and B' or, equivalently, a 2x3x4 array containing A' and B'. Any idea?

Answer: I think you can use np.einsum np.einsum("ij, jkl -> ikl", C, Z)  where "ij, jkl -> ikl" specifies the contraction pattern, where i and j are the indices of the C matrix, and j, k, and l are the indices of the Z array.  Example Given dummy data like below np.random.seed(0) Z = np.random.normal(0.0, 1.0, [2, 3, 4]) C = [[1,2],[3,4]]  You will see print("AB_prim(einsum): \n", np.einsum("ij, jkl -> ikl", C, Z))  shows AB_prim(einsum): [[[ 3.2861278 0.64350724 1.86646445 2.90824185] [ 4.85571614 -1.38759441 1.57622382 -1.85954869] [ -5.20919848 1.71783569 1.87291597 -0.03005653]] [[ 8.33630794 1.68717169 4.71166688 8.05737691] [ 11.57899026 -3.75246669 4.10253606 -3.87045458] [-10.52161582 3.84626989 3.88987551 1.39416044]]]  and A, B = Z[0], Z[1] print("A_prim: \n", C[0][0] * A + C[0][1] * B) print("B_prim: \n", C[1][0] * A + C[1][1] * B)  shows A_prim: [[ 3.2861278 0.64350724 1.86646445 2.90824185] [ 4.85571614 -1.38759441 1.57622382 -1.85954869] [-5.20919848 1.71783569 1.87291597 -0.03005653]] B_prim: [[ 8.33630794 1.68717169 4.71166688 8.05737691] [ 11.57899026 -3.75246669 4.10253606 -3.87045458] [-10.52161582 3.84626989 3.88987551 1.39416044]]
Question: I want to run a suite of unit tests in the tests folder. The basic code is: suite = unittest.defaultTestLoader.discover('tests')  I want only some of these tests to run, for example test_e1 if file e1.py is present, test_e5 if e5.py is present, but not test_e2 and test_e11 (because files e2.py and e11.py are missing). I tried the pattern argument of the discoverer() function, which defaults to test_*.py, but it does not allow enough control for what I need (see How to match specific files with a shell pattern in unit test discoverer? ). One answer in that thread suggests finding these tests with unittest.TestLoader().loadTestsFromNames, so I tried this code:  file_list = [] for some_file in some_file_list: full_filepath = os.path.join(some_dir, some_file) if not os.path.exists(full_filepath): continue file_list.append("tests/test_%s.TestDocs" % some_file) suite = unittest.TestLoader().loadTestsFromNames(file_list) print(suite)  The name TestDocs is the class name that inherits from the unit test: class TestDocs(unittest.TestCase):  But this shows a list of failed tests such as: <unittest.suite.TestSuite tests=[<unittest.loader._FailedTest testMethod=tests/test_>]>  How can I run tests only for a certain set of files?

Answer: You are passing hybrid file-path/object names to loadTestsFromNames. Drop the tests from the name, and ensure that tests appears on your module search path, either by  Modifying sys.path before calling the method, or Adding tests to the PYTHONPATH environment variable before running your tests.
Question: I am building some configuration logic for a Python 3 app, and trying to use pydantic and pydantic-settings to manage validation etc. I'm able to load raw settings from a YAML file and create my settings object from them. I'm also able to read a value from an environment variable. But I can't figure out how to make the environment variable value take precedence over the raw settings: import os import yaml as pyyaml from pydantic_settings import BaseSettings, SettingsConfigDict class FooSettings(BaseSettings): foo: int bar: str model_config = SettingsConfigDict(env_prefix='FOOCFG__') raw_yaml = """ foo: 13 bar: baz """ os.environ.setdefault("FOOCFG__FOO", "42") raw_settings = pyyaml.safe_load(raw_yaml) settings = FooSettings(**raw_settings) assert settings.foo == 42  If I comment out foo: 13 in the input yaml, the assertion passes. How can I make the env value take precedence?

Answer: Are you sure you want the environment to take precedence? While not ubiquitous, it is very common for environment variables to have the lowest precedence (typically, the ordering is built-in defaults, then environment variables, then configuration files, then command line options). Deviating from this convention can be surprising. You could get the behavior you want for a specific field by adding a field validator that checks for the appropriate environment variable and uses that value in preference to an existing value if it is avaiable. Something like: import os import yaml as pyyaml from pydantic_settings import BaseSettings, SettingsConfigDict from pydantic import field_validator class FooSettings(BaseSettings): model_config = SettingsConfigDict(env_prefix="FOOCFG__") foo: int bar: str @field_validator("foo", mode="after") @classmethod def validate_foo(cls, val): '''Always use the value from the environment if it's available.''' if env_val := os.environ.get(f"{cls.model_config['env_prefix']}FOO"): return int(env_val) return val raw_yaml = """ foo: 13 bar: baz """ os.environ.setdefault("FOOCFG__FOO", "42") raw_settings = pyyaml.safe_load(raw_yaml) settings = FooSettings(**raw_settings) assert settings.foo == 42  If you wanted to do this for all fields, you could use a model validator instead. Maybe something like this?  @model_validator(mode="after") @classmethod def validate_foo(cls, data): for field in cls.model_fields: env_name = f'{cls.model_config["env_prefix"]}{field.upper()}' if env_val := os.environ.get(env_name): setattr(data, field, type(getattr(data, field))(env_val)) return data
Question: I get error if I don't supply retain_graph=True in y1.backward()  import torch x = torch.tensor([2.0], requires_grad=True) y = torch.tensor([3.0], requires_grad=True) f = x+y z = 2*f y1 = z**2 y2 = z**3 y1.backward() y2.backward()  Traceback (most recent call last): File "/Users/a0m08er/pytorch/pytorch_tutorial/tensor.py", line 58, in <module> y2.backward() File "/Users/a0m08er/pytorch/lib/python3.11/site-packages/torch/_tensor.py", line 521, in backward torch.autograd.backward( File "/Users/a0m08er/pytorch/lib/python3.11/site-packages/torch/autograd/__init__.py", line 289, in backward _engine_run_backward( File "/Users/a0m08er/pytorch/lib/python3.11/site-packages/torch/autograd/graph.py", line 769, in _engine_run_backward return Variable._execution_engine.run_backward( # Calls into the C++ engine to run the backward pass ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RuntimeError: Trying to backward through the graph a second time (or directly access saved tensors after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad(). Specify retain_graph=True if you need to backward through the graph a second time or if you need to access saved tensors after calling backward.  But I don't get error when I do this:  import torch x = torch.tensor([2.0], requires_grad=True) y = torch.tensor([3.0], requires_grad=True) z = x+y y1 = z**2 y2 = z**3 y1.backward() y2.backward()  Since z is a common node for y1 and y2 why it is not showing me error when I do y2.backward()

Answer: basically the error  Trying to backward through the graph a second time (or directly access saved tensors after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad(). Specify retain_graph=True if you need to backward through the graph a second time or if you need to access saved tensors after calling backward.  Error comes when the backwards pass tries to access tensors that were saved for the backwards pass (using ctx.save_for_backward), and those are not present (usually because they were freed after doing the first backward pass witoutretain_graph=True). So the computation graph is still there after the first backwards pass, only the tensors saved in context were freed. But the thing is, addition operations do not need to save tensors for backwards pass (the gradient along each of the inputs is the same as the gradient over the sum — so the gradient is just passed along the graph without doing any operation, no need to save anything for backward). Thus the error doesn't happen if the only shared node is an addition node. In comparison, multiplication needs to save the input values for the backward pass (since the gradient for a * b along b is a * grad(a * b)). Thus the exception gets raised when it tries to access them
Question: I am reading a text file only containing floating point numbers using numpy.loadtxt. However, some of the data is corrupted and reads something like X.XXXXXXX+YYY instead of X.XXXXXXXE+YY (Missing E char). I'd like to interpret them as the intended floating point number (or NaN if impossible) and wondered if there was any easy way to do this upon reading the file instead of manually correcting each entries in the file since it contains hundreds of thousands of lines of data. MWE: import numpy as np data = np.loadtxt("path/to/datafile")  Example of error raised: ValueError: could not convert string '0.710084093014+195' to float64 at row 862190, column 6

Answer: The following works: import numpy as np import re def converter(txt): txt = re.sub(r"(?<=\d)(?<!E)[\+\-]", lambda x: 'E'+x[0], txt.decode()) return float(txt) np.loadtxt("path/to/datafile", converters = converter)
Question: I've been trying to create this machine learning tool to make predictions on the amount of orders in the next year per month but I have been getting this error: ValueError: to assemble mappings requires at least that [year, month, day] be specified: [month] is missing  here is my code. I am passing in the month and it should be getting assigned a number that is supposed to represent the respective month, but form some reason this does not appear to be happening. I am also aware that the months are not all capitalized but this should not be an issue as they are all getting passed to lowercase. import pandas as pd # Example DataFrame creation from CSV (replace this with your actual CSV upload logic) data = { 'Year': [2021, 2021, 2021, 2022, 2022, 2023, 2023], 'Month': ['january', 'february', 'march', 'january', 'february', 'march', 'april'], 'OrderCount': [60, 55, 70, 64, 56, 76, 70] } df = pd.DataFrame(data) # Convert 'Month' to numerical values (January = 1, February = 2, etc.) month_map = { 'january': 1, 'february': 2, 'march': 3, 'april': 4, 'may': 5, 'june': 6, 'july': 7, 'august': 8, 'september': 9, 'october': 10, 'november': 11, 'december': 12 } # Map month names to numbers df['Month'] = df['Month'].str.lower() df['MonthNum'] = df['Month'].map(month_map) # Convert Year and MonthNum to integers df['Year'] = df['Year'].astype(int) df['MonthNum'] = df['MonthNum'].astype(int) # Combine Year and Month into a DateTimeIndex # The next line is where the issue is likely occurring df['Date'] = pd.to_datetime(df[['Year', 'MonthNum']].assign(DAY=1)) # Print the resulting DataFrame to see if 'Date' was successfully created print(df) 

Answer: If you check to_datetime documentation, you will find that it requires the column called month. Your month column contains the month names. You should rename the columns before using to_datetime like this: df=df.rename(columns={"Month": "MonthName", "MonthNum": "Month"}). This way, pandas will look for the month numeric column and find it.
Question: I have a question regarding the precision of float in JAX. For the following code, import numpy as np import jax.numpy as jnp print('jnp.arctan(10) is:','%.60f' % jnp.arctan(10)) print('np.arctan(10) is:','%.60f' % np.arctan(10)) jnp.arctan(10) is: 1.471127629280090332031250000000000000000000000000000000000000 np.arctan(10) is: 1.471127674303734700345103192375972867012023925781250000000000 print('jnp.arctan(10+1e-7) is:','%.60f' % jnp.arctan(10+1e-7)) print('np.arctan(10+1e-7) is:','%.60f' % np.arctan(10+1e-7)) jnp.arctan(10+1e-7) is: 1.471127629280090332031250000000000000000000000000000000000000 np.arctan(10+1e-7) is: 1.471127675293833592107262120407540351152420043945312500000000  jnp gave identical results for arctan(x) for a small change of input variable (1e-7), but np did not. My question is how to let jax.numpy get the right number for a small change of x? Any comments are appreciated.

Answer: JAX defaults to float32 computation, which has a relative precision of about 1E-7. This means that your two inputs are effectively identical: >>> np.float32(10) == np.float32(10 + 1E-7) True  If you want 64-bit precision like NumPy, you can enable it as discussed at JAX sharp bits: double precision, and then the results will match to 64-bit precision: import jax jax.config.update('jax_enable_x64', True) import jax.numpy as jnp import numpy as np print('jnp.arctan(10) is:','%.60f' % jnp.arctan(10)) print('np.arctan(10) is: ','%.60f' % np.arctan(10)) print('jnp.arctan(10+1e-7) is:','%.60f' % jnp.arctan(10+1e-7)) print('np.arctan(10+1e-7) is: ','%.60f' % np.arctan(10+1e-7))  jnp.arctan(10) is: 1.471127674303734700345103192375972867012023925781250000000000 np.arctan(10) is: 1.471127674303734700345103192375972867012023925781250000000000 jnp.arctan(10+1e-7) is: 1.471127675293833592107262120407540351152420043945312500000000 np.arctan(10+1e-7) is: 1.471127675293833592107262120407540351152420043945312500000000  (but please note that even the 64-bit precision used by Python and NumPy is only accurate to about one part in 10^16, so most of the digits in the representation you printed are inaccurate compared to the true arctan value).
Question: In Polars / pandas I can do a rolling sum where row each row the window is (row - 10 minutes, row]. For example: import polars as pl data = { "timestamp": [ "2023-08-04 10:00:00", "2023-08-04 10:05:00", "2023-08-04 10:10:00", "2023-08-04 10:10:00", "2023-08-04 10:20:00", "2023-08-04 10:20:00", ], "value": [1, 2, 3, 4, 5, 6], } df = pl.DataFrame(data).with_columns(pl.col("timestamp").str.strptime(pl.Datetime)) print( df.with_columns(pl.col("value").rolling_sum_by("timestamp", "10m", closed="right")) )  This outputs shape: (6, 2) ┌─────────────────────┬───────┐ │ timestamp ┆ value │ │ --- ┆ --- │ │ datetime[μs] ┆ i64 │ ╞═════════════════════╪═══════╡ │ 2023-08-04 10:00:00 ┆ 1 │ │ 2023-08-04 10:05:00 ┆ 3 │ │ 2023-08-04 10:10:00 ┆ 9 │ │ 2023-08-04 10:10:00 ┆ 9 │ │ 2023-08-04 10:20:00 ┆ 11 │ │ 2023-08-04 10:20:00 ┆ 11 │ └─────────────────────┴───────┘  How can I do this in DuckDB? Closest I could come up with is: rel = duckdb.sql(""" SELECT timestamp, value, SUM(value) OVER roll AS rolling_sum FROM df WINDOW roll AS ( ORDER BY timestamp RANGE BETWEEN INTERVAL 10 minutes PRECEDING AND CURRENT ROW ) ORDER BY timestamp; """) print(rel)  but that makes the window [row - 10 minutes, row], not (row - 10 minutes, row] Alternatively, I could do rel = duckdb.sql(""" SELECT timestamp, value, SUM(value) OVER roll AS rolling_sum FROM df WINDOW roll AS ( ORDER BY timestamp RANGE BETWEEN INTERVAL '10 minutes' - INTERVAL '1 microsecond' PRECEDING AND CURRENT ROW ) ORDER BY timestamp; """)  but I'm not sure about how robust that'd be?

Answer: Maybe not particularly neat, but from the top of my head you could exclude the rows which are exactly 10 minutes back by additional window clause import duckdb rel = duckdb.sql(""" SELECT timestamp, value, SUM(value) OVER roll - coalesce(SUM(value) OVER exclude, 0) AS rolling_sum FROM df WINDOW roll AS ( ORDER BY timestamp RANGE BETWEEN INTERVAL 10 minutes PRECEDING AND CURRENT ROW ), exclude AS ( ORDER BY timestamp RANGE BETWEEN INTERVAL 10 minutes PRECEDING AND INTERVAL 10 minutes PRECEDING ) ORDER BY timestamp; """) print(rel)  ┌─────────────────────┬───────┬─────────────┐ │ timestamp │ value │ rolling_sum │ │ timestamp │ int64 │ int128 │ ├─────────────────────┼───────┼─────────────┤ │ 2023-08-04 10:00:00 │ 1 │ 1 │ │ 2023-08-04 10:05:00 │ 2 │ 3 │ │ 2023-08-04 10:10:00 │ 3 │ 9 │ │ 2023-08-04 10:10:00 │ 4 │ 9 │ │ 2023-08-04 10:20:00 │ 5 │ 11 │ │ 2023-08-04 10:20:00 │ 6 │ 11 │ └─────────────────────┴───────┴─────────────┘
Question: I want to loop through an array with an if statement and only after it has looped through the entire array execute the else. This is how i have my code now for index, nameList in enumerate(checkedName) if record["first_name"] == nameList["first_name"] and record["last_name"] == nameList["last_name"]: print("Match name") else: print("No match name") checkedName.append({"id" : record["id"], "first_name" : record["first_name"], "last_name" : record["last_name"]})  But i would like it to be more like this: for index, nameList in enumerate(checkedName) if record["first_name"] == nameList["first_name"] and record["last_name"] == nameList["last_name"]: print("Match name") else: print("No match name") checkedName.append({"id" : record["id"], "first_name" : record["first_name"], "last_name" : record["last_name"]})  But i have no idea how to do this in a not so messy way i have an idea but i feel like i could do this shorter

Answer: Using any() is more pythonic and readable...Logic is like check if any record in the list matches your conditions if any(record["first_name"] == nameList["first_name"] and record["last_name"] == nameList["last_name"] for nameList in checkedName): print("Match name") else: print("No match name") checkedName.append({ "id": record["id"], "first_name": record["first_name"], "last_name": record["last_name"] })
Question: The dir() function prints the methods in alphabetical order. Is there a way to get the methods of a class (with their parameters) but keeping the original order of the methods? Here's my code return [ (m, getattr(PythonClass, m).__code__.co_varnames) for m in dir(PythonClass) ] 

Answer: As @Barmar mentioned in the comments, you can use the __dict__ attribute of a class to access its attributes. Since Python 3.7 dict keys are guaranteed to retain their insertion order, so by iterating over PythonClass.__dict__ you can obtain attributes of PythonClass in the order of definition. It is also more idiomatic to use the vars function instead of the __dict__ attribute to access the attributes dict. To filter class attributes for methods, you can use inspect.isfunction to test if an attribute is a function: from inspect import isfunction class PythonClass: var = 1 def method_b(self, b): ... def method_a(self, a): ... print([ (name, obj.__code__.co_varnames) for name, obj in vars(PythonClass).items() if isfunction(obj) ])  This outputs: [('method_b', ('self', 'b')), ('method_a', ('self', 'a'))]  Demo: https://ideone.com/rNMcYO
Question: Have been told that in Python '''  is used to indicate the start of a multi-line string. However I have also been taught that this code also allows for the documentation of functions and modules. Googling, surprisingly, doesn't give a clear answer on what ''' definitively refers to. So how should I remember, as a beginner, what this Python code refers to? A multi-line string? An operator to assist documentation? Both? Something else?

Answer: Triple quotes ''' (and """) is a marker for a string literal just like quote characters ' and ", which you can see in Python's grammar for String and Bytes literals. Its only difference to regular quotes is that newlines and unescaped quote characters are allowed within a triple-quoted string literal, which makes triple quotes ideal for documentation in natural language where newlines and unescaped quote characters can appear often. It is why as a convention triple quotes are used for docstrings, as suggested in PEP-257, although you can still use regular quotes for docstrings.
Question: I am trying to extract a cell value from a dataframe, then why I always get a series instead of a value. For example: df_test=pd.DataFrame({'Well':['test1','test2','test3'],'Region':['east','west','east']}) df_test Well Region 0 test1 east 1 test2 west 2 test3 eas well='test2' region_thiswell=df_test.loc[df_test['Well']==well,'Region'] region_thiswell 1 west Name: Region, dtype: object  I am expecting variable of region_thiswell is equal to 'west' string only. Why I am getting a series? Thanks

Answer: A potential issue with item/values/iloc is that it will yield an exception of there is no match. squeeze will return an empty Series: df_test.loc[df_test["Well"] == 'test999', "Region"].item() # ValueError: can only convert an array of size 1 to a Python scalar df_test.loc[df_test["Well"] == 'test999', "Region"].values[0] # IndexError: index 0 is out of bounds for axis 0 with size 0 df_test.loc[df_test["Well"] == 'test999', "Region"].iloc[0] # IndexError: single positional indexer is out-of-bounds df_test.loc[df_test["Well"] == 'test999', "Region"].squeeze() # Series([], Name: Region, dtype: object)  One robust approach to get a scalar would be to use next+iter: next(iter(df_test.loc[df_test["Well"] == 'test2', "Region"]), None) # 'west' next(iter(df_test.loc[df_test["Well"] == 'test999', "Region"]), None) # None  In case of multiple matches you'll get the first one: next(iter(df_test.loc[df_test["Well"].str.startswith('test'), "Region"]), None) # 'east'  Alternatively, but more verbose and less efficient: df_test.loc[df_test["Well"] == 'test999', "Region"].reset_index().get(0)