eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I just installed a newer version of a package in a poetry project. Now, for every package listed in the poetry.lock file, there's an added files key, like this: [[package]] name = "..." version = "..." files = [ {...}, {...} ]  This wasn't there before, only after I installed the new version of the package, and introduces a lot of changes to the poetry.lock. What is this files key and why was it created now when it wasn't there before? Can I prevent its creation?

Answer: There is little documentation on Poetry's lock file format and its changes from version to version. Regarding your question, there is a discussion on Github that briefly describes what's going on: Before lock file format version 2.0 The files entry was its own section in the lock file, so one could find, for example, the following structure: [[package]] name = "matplotlib" version = ... description = ... category = ... optional = ... python-versions = ... ... [metadata.files] matplotlib = [ {file = "matplotlib-XXX.whl", hash = "sha256:XXX"}, ... ]  With lock file format version 2.0 The files entry is now part of each package's section, so a corresponding entry looks, for example, as follows: [[package]] name = "matplotlib" version = ... description = ... category = ... optional = ... python-versions = ... files = [ {file = "matplotlib-XXX.whl", hash = "sha256:XXX"}, ... ]  Information-wise, both representations are equivalent, just the structure is different. Meaning of the files entries To my understanding, the entries are created to ensure that the content of a package that is about to be installed has not changed since the lock file creation. This is to ensure the full reproducibility of the project's environment at reinstallation. That's why the entries consist of file-hash pairs:  file is the wheel (.whl file) that represents the Python package: usually there are different wheels for different platforms and Python versions, which is why there is a list of entries for most packages; hash is the sha256 checksum for the wheel.  As to your last question: I don't think it is possible to prevent the creation of the files entries. Also, given that (a) the corresponding information serves a vital purpose of the lock file and (b) the same information has been stored before albeit in a different place, I don't think it would make much sense to do so.
Question: class Foo: bar: str class Bat(Foo): @property def bar(self) -> str: ...  Given the above code, my typechecker (mypy) raises the following complaint: error: Signature of "bar" incompatible with supertype "Foo" [override]  This surprises me given that an instance of Foo or Bat will behave the same from the perspective of a caller accessing the bar attribute/property. What is the issue the typechecker is preventing by rejecting this code?

Answer: Expanding on the comments to the OP: Older versions of Mypy had some sort of issue/bug somehow tangentially related to this that led to some discussions on the project's GitHub: Mypy disallows overriding an attribute with a property and that should have be fixed on versions >=v0.990 There's also a discussion which feels much closer to the current's OP exposition: overriding variable with property: error Signature incompatible with supertype In this second case, what happens is that... class Foo: bar: str  ...tells Mypy that .bar will be a writable attribute, whereas just declaring the @property in the child class Bat... class Bat(Foo): @property def bar(self) -> str:  ... would make the attribute read-only. The most straightforward fix, in this case, might be creating a setter for .bar. The following code: class Foo: bar: str class Bat(Foo): @property def bar(self) -> str: return "something" @bar.setter def bar(self, value: str): # ... actual setter code probably # for self._bar or something like that pass  Produces: Success: no issues found in 1 source file
Question: I have an array A defined as: [[0, 0], [0, 0]]  I have a list I containing indices of A, for example [(0, 1), (1, 0), (1, 1)], and a list v with as many values as in I, for example [1, 2, 3]. I want to replace entries of A at indices contained in I with corresponding values stored in v. The expected result is therefore: [[0, 1], [2, 3]]  I was expecting to be able to achieve this using np.put. I tried: np.put(A, I, v)  However the value of B after running the line above is: [[1., 3.], [0., 0.]]  Why did put behave this way and how can I achieve the result I was expecting?

Answer: IIUC, you can do: I = np.array(I) A[I[:, 0], I[:, 1]] = v print(A)  Prints: [[0 1] [2 3]]
Question: In the Pandas documentation here for Series.isin(values), they state:  values : set or list-like  What is considered list-like? For a Python dictionary temp_dict, would temp_dict.keys() and temp_dict.values() be considered list-like?

Answer: "List-like" isn't a standard Python term. Googling pandas list-like turns up pandas.api.types.is_list_like, but the documentation for that just says  Check if the object is list-like. Objects that are considered list-like are for example Python lists, tuples, sets, NumPy arrays, and Pandas Series. Strings and datetime objects, however, are not considered list-like.  which isn't really much of a spec. So, as a last resort, we turn to the source code, and after following a lot of imports and aliasing, we eventually find this function: cdef bint c_is_list_like(object obj, bint allow_sets) except -1: # first, performance short-cuts for the most common cases if util.is_array(obj): # exclude zero-dimensional numpy arrays, effectively scalars return not cnp.PyArray_IsZeroDim(obj) elif isinstance(obj, list): return True # then the generic implementation return ( # equiv: `isinstance(obj, abc.Iterable)` getattr(obj, "__iter__", None) is not None and not isinstance(obj, type) # we do not count strings/unicode/bytes as list-like # exclude Generic types that have __iter__ and not isinstance(obj, (str, bytes, _GenericAlias)) # exclude zero-dimensional duck-arrays, effectively scalars and not (hasattr(obj, "ndim") and obj.ndim == 0) # exclude sets if allow_sets is False and not (allow_sets is False and isinstance(obj, abc.Set)) )  So Pandas considers an object list-like if it passes this complicated series of checks.  If an object is a 0-dimensional NumPy array, it's not list-like. Otherwise, if it's a list, it's list-like. Otherwise, it needs to pass all the following checks to be list-like:  It needs to have an __iter__ attribute that's not None. It needs to not be a type. It needs to not be a string, a bytestring, or a "generic alias" (a type used for some typing module things). It needs to not have an ndim attribute equal to 0. In some cases, Pandas will disallow instances of collections.abc.Set, which are sets, frozensets, and certain other set-like objects. (abc is collections.abc here.)    That means Pandas considers most iterable objects to be list-like. Strings, bytestrings, generic aliases, and iterable type objects (like Enum classes) are excluded, with that part about excluding iterable type objects probably being a bug - the code is trying to exclude non-iterable type objects whose instances are iterable. The 0-dimensional array and ndim==0 checks attempt to exclude objects for which positive-dimensional instances of their type would be iterable, but 0-dimensional instances aren't. Sets and other collections.abc.Set subclasses are sometimes excluded, but Series.isin doesn't pass the flag to exclude them.
Question: I type a time like 7:30 run it through the convert() function to turn that into a float that would equal 7.5. Then call it back to main() and check what type it is now, and it gives me it's still a str not a float. def main(): meal_time = input("What time is it?").strip() convert(meal_time) print(type(meal_time)) def convert(time): hour, minu = time.split(":") hour = float(hour) #7.0 minu = float(minu) / 60 return float(hour+minu) if __name__ == "__main__": main()  Input 7:30  Output type 'str' 

Answer: You're not updating meal_time in main() with the float returned from convert(). Functions in Python don't modify the original variable unless it's mutable and you're explicitly changing it. Instead of: convert(meal_time)  Try: meal_time = convert(meal_time)  Output you should observe after the above change: <class 'float'>
Question: I have been playing around with oobabooga text-generation-webui  on my Ubuntu 20.04 with my NVIDIA GTX 1060 6GB for some weeks without problems. I have been using llama2-chat models sharing memory between my RAM and NVIDIA VRAM. I installed without much problems following the intructions on its repository. So what I want now is to use the model loader llama-cpp with its package llama-cpp-python bindings to play around with it by myself. So using the same miniconda3 environment that oobabooga text-generation-webui uses I started a jupyter notebook and I could make inferences and everything is working well BUT ONLY for CPU. A working example bellow, from llama_cpp import Llama llm = Llama(model_path="/mnt/LxData/llama.cpp/models/meta-llama2/llama-2-7b-chat/ggml-model-q4_0.bin", n_gpu_layers=32, n_threads=6, n_ctx=3584, n_batch=521, verbose=True), prompt = """
 
 Name the planets in the solar system? 
 
 """ output = llm(prompt, max_tokens=350, echo=True) print(output['choices'][0]['text'].split('
')[-1])   Of course! Here are the eight planets in our solar system, listed in order from closest to farthest from the Sun:  Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune    Note that Pluto was previously considered a planet but is now classified as a dwarf planet due to its small size and unique orbit.  I want to make inference using GPU as well. What is wrong? Why can't I offload to gpu like the parameter n_gpu_layers=32 specifies and also like oobabooga text-generation-webui already does on the same miniconda environment whithout any problems?

Answer: After searching around and suffering quite for 3 weeks I found out this issue on its repository. The llama-cpp-python needs to known where is the libllama.so shared library. So exporting it before running my python interpreter, jupyter notebook etc. did the trick. For using the miniconda3 installation used by oobabooga text-generation-webui I exported it like bellow: export LLAMA_CPP_LIB=/yourminicondapath/miniconda3/lib/python3.10/site-packages/llama_cpp_cuda/libllama.so  Voilà!!!! On importing from llama_cpp import Llama I get  ggml_init_cublas: found 1 CUDA devices: Device 0: NVIDIA GeForce GTX 1060, compute capability 6.1  And on llm = Llama(model_path="/mnt/LxData/llama.cpp/models/meta-llama2/llama-2-7b-chat/ggml-model-q4_0.bin", n_gpu_layers=28, n_threads=6, n_ctx=3584, n_batch=521, verbose=True),  ...  llama_model_load_internal: using CUDA for GPU acceleration llama_model_load_internal: mem required = 2381.32 MB (+ 1026.00 MB per state) llama_model_load_internal: allocating batch_size x (512 kB + n_ctx x 128 B) = 480 MB VRAM for the scratch buffer llama_model_load_internal: offloading 28 repeating layers to GPU llama_model_load_internal: offloaded 28/35 layers to GPU llama_model_load_internal: total VRAM used: 3521 MB ...
Question: I'm having a bit of trouble with my python code. I originally wrote it using pandas, but I need something a bit faster, so I'm converting it to polars. After reading the mongodb into polars dataframes with race = pl.DataFrame(list(race_coll.find()))  and converting the 'date_of_race' column into pl.Date type using race = race.with_columns(pl.col('date_of_race').str.strptime(pl.Date, format='%d %m %Y').cast(pl.Date))  The pandas code that worked was days_between = (pd.to_datetime('today') - race.date_of_race.values[0]) // np.timedelta64(1,'D')  I have tried the following: date = pl.DataFrame({"date_of_race": [1], "value": race['date_of_race']}) days_between = (pd.to_datetime('today').normalize() - days_between[0][0]) // np.timedelta64(1,'D') TypeError: 'int' object is not subscriptable  days_between = (pd.to_datetime('today').normalize() - race['date_of_race']) // np.timedelta64(1,'D') PanicException: cannot coerce datatypes: ComputeError(ErrString("failed to determine supertype of object and date"))  When I print the dates, I get the following: pandas: print(race.date_of_race.values[0]) 2022-10-15T00:00:00.000000000 polars: print(race['date_of_race']) shape: (1,) Series: 'date_of_race' [date] [ 2022-10-15 ]  Any help is appreciated

Answer: use a Python datetime object for the reference date, and .dt.total_days() to get the days difference. EX: import polars as pl import pandas as pd s = pl.Series([ "2022-10-30T00:00:00", "2022-10-30T01:00:00", "2022-10-30T02:00:00", "2022-10-30T03:00:00", "2022-10-30T04:00:00", "2022-10-30T05:00:00", ]).cast(pl.Datetime) diff = pd.to_datetime('today').normalize().to_pydatetime() - s # could also use the datetime module's date class here via # datetime.today().date() print(diff) # Series: '' [duration[μs]] # [ # 299d # 298d 23h # 298d 22h # 298d 21h # 298d 20h # 298d 19h # ] print(diff.dt.total_days()) # Series: '' [i64] # [ # 299 # 298 # 298 # 298 # 298 # 298 # ]
Question: Given a polars DataFrame column like df = pl.DataFrame({"a": [0, 29, 28, 4, 0, 0, 13, 0]})  how to get a new column like shape: (8, 2) ┌─────┬──────┐ │ a ┆ dist │ │ --- ┆ --- │ │ i64 ┆ i64 │ ╞═════╪══════╡ │ 0 ┆ 1 │ │ 29 ┆ 0 │ │ 28 ┆ 0 │ │ 4 ┆ 0 │ │ 0 ┆ 1 │ │ 0 ┆ 2 │ │ 13 ┆ 0 │ │ 0 ┆ 1 │ └─────┴──────┘  The solution should preferably work with .over() for grouped values and optionally an additional rolling window function like rolling_mean(). I know of the respective question for pandas but couldn't manage to translate it.

Answer: Here's one way with rle_id to identify the groups to project over, and only doing so on the 0 groups with a when/then: df = pl.DataFrame({"a": [0, 29, 28, 4, 0, 0, 13, 0]}) df.with_columns( dist=pl.when(pl.col('a') == 0) .then(pl.col('a').cum_count().over(pl.col('a').ne(0).rle_id())) .otherwise(0) )  shape: (8, 2) ┌─────┬──────┐ │ a ┆ dist │ │ --- ┆ --- │ │ i64 ┆ u32 │ ╞═════╪══════╡ │ 0 ┆ 1 │ │ 29 ┆ 0 │ │ 28 ┆ 0 │ │ 4 ┆ 0 │ │ 0 ┆ 1 │ │ 0 ┆ 2 │ │ 13 ┆ 0 │ │ 0 ┆ 1 │ └─────┴──────┘
Question: I have a csv file with a size of 28 GB, which I want to plot. Those are way too many data points obviously, so how can I reduce the data? I would like to merge about 1000 data points into one by calculating the mean. This is the sturcture of my DataFrame: df = pl.from_repr(""" ┌─────────────────┬────────────┐ │ Time in seconds ┆ Force in N │ │ --- ┆ --- │ │ f64 ┆ f64 │ ╞═════════════════╪════════════╡ │ 0.0 ┆ 2310.18 │ │ 0.0005 ┆ 2313.23 │ │ 0.001 ┆ 2314.14 │ └─────────────────┴────────────┘ """)  I thought about using group_by_dynamic, and then calculating the mean of each group, but this only seems to work when using datetimes? The time in seconds is given as a float however.

Answer: You can also group by an integer column to create groups of size N:  In case of a group_by_dynamic on an integer column, the windows are defined by: “1i” # length 1 “10i” # length 10  We can add a row index and cast to pl.Int64 to use it. (df.with_row_index() .group_by_dynamic(pl.col.index.cast(pl.Int64), every="2i") .agg("force") )  shape: (4, 2) ┌───────┬────────────┐ │ index ┆ force │ │ --- ┆ --- │ │ i64 ┆ list[str] │ ╞═══════╪════════════╡ │ 0 ┆ ["A", "B"] │ │ 2 ┆ ["C", "D"] │ │ 4 ┆ ["E", "F"] │ │ 6 ┆ ["G"] │ └───────┴────────────┘
Question: I'm type-hinting functions in Python, and not sure what type a matplotlib color should be. I have a function like this: def plot_my_thing(data: np.ndarray, color: ???): # function def here  What should the type be in ???, when it is meant to be a matplotlib color that you can feed into plt.plot() for the type hint? Right now I'm planning to just use Any. I've searched and not found an answer. There are some discussions at GitHub about it: https://github.com/matplotlib/matplotlib/issues/21505 But that seems like a package-specific problem, though I may not be understanding it.

Answer: Matplotlib introduced type hints in 3.8.0. Now, you can use matplotlib.typing.ColorType which is just an alias for all valid matplotlib colors. For more information, see the matplotlib typing api reference.
Question: I stuck a bit with hiding decimals of Money object. Ex: In [51]: str(Money(123, 'USD')) Out[51]: 'US$123.00'  returns with 00 in the end. Before it was resolved by money_obj.decimal_places_display = 0 but it is deleted in the last version on djmoney https://django-money.readthedocs.io/en/latest/changes.html#id3 I have tried to use babel format_currency, but no success. The decimals are there so far: In [54]: from babel.numbers import format_currency ...: format_currency(12345, 'USD', format='¤#') Out[54]: '$12345.00'  For now my solution is quite manual, and the question is it possible to make it better? In [55]: from babel.numbers import format_decimal ...: from djmoney.money import Money ...: ...: from utils.constants import CURRENCIES_CHOICES ...: ...: ...: def format_int(money: Money) -> str: ...: amount = format_decimal(round(money.amount), locale='en_GB') ...: currency = format_currency(0, str(money.currency), format='¤') ...: return f'{currency} {amount}' ...: ...: format_int(Money(12345, 'USD')) ...: Out[55]: '$ 12,345' 

Answer: babel's format_currency allows us to display the rounded amount by passing a format string and currency_digits=False. However, by using a format string we lose the ability to localize the amount and the placement of the currency sign. Example: # Python 3.12.2 # Babel==2.14.0 from babel.numbers import format_currency # This is OK, since in en_GB the currency sign goes before the amount format_currency( currency='USD', currency_digits=False, format='¤#', locale='en_GB', number=42.24, ) # US$42 # This overrides the placement of the currency sign in fr_FR format_currency( currency='USD', currency_digits=False, format='¤#', locale='fr_FR', number=42.24, ) # $US42  If we omit the format string, then we see that in French the currency symbol should go after the amount: format_currency( currency='USD', currency_digits=False, locale='fr_FR', number=42.24, ) # 42,24\xa0$US  We can round the amount and keep the localized format by using a helper function, e.g. the function published in this comment or an adapted version which always rounds the amount: from babel import Locale def format_currency_no_decimals( currency, number, locale, **kwargs, ): locale_ = Locale.parse(locale) pattern = locale_.currency_formats["standard"] return pattern.apply( number, locale_, currency=currency, force_frac=(0, 0), **kwargs ) format_currency_no_decimals( currency='USD', locale='en_GB', number=42.24, ) # US$42 format_currency_no_decimals( currency='USD', locale='fr_FR', number=42.24, ) # 42\xa0$US  Note: we will get a deprecation warning when calling pattern.apply: DeprecationWarning: The force_frac parameter to NumberPattern.apply() is deprecated., so this solution isn't future proof. We can also override the currency symbol for a specific locale, e.g.: from babel import Locale Locale.parse("en_GB").currency_symbols["USD"] = "$" format_currency(currency='USD', locale='en_GB', number=42.24) # $42.24 format_currency_no_decimals(currency='USD', locale='en_GB', number=42.24) # $42
Question: I have a dataframe that looks like this: Container Event A Clean B Dry A Clean A Dry B Clean C Clean C Clean C Clean  I want to introduce a new column called 'Temperature', which has the value 4 the first time a container has the event 'Clean' and value 3 for all subsequent 'Clean' events. Dry would always have value 1. The dataframe should look like this: Container Event Temperature A Clean 4 B Dry 1 A Clean 3 A Dry 1 B Clean 4 C Clean 4 C Clean 3 C Clean 3  Reproducible dataframe: d = {'Container': ['A','B','A','A','B','C','C','C'], 'Event': ['Clean', 'Dry', 'Clean', 'Dry', 'Clean', 'Clean', 'Clean', 'Clean']} df = pd.DataFrame(data=d) 

Answer: make conditions and use np.select import numpy as np cond1 = ~df.duplicated() cond2 = df['Event'].eq('Clean') cond3 = df['Event'].eq('Dry') df['Temperature'] = np.select([cond1 & cond2, cond2, cond3], [4, 3, 1])  df Container Event Temperature 0 A Clean 4 1 B Dry 1 2 A Clean 3 3 A Dry 1 4 B Clean 4 5 C Clean 4 6 C Clean 3 7 C Clean 3   If there are only dry and clean, cond3 does not need to be created. import numpy as np cond1 = ~df.duplicated() cond2 = df['Event'].eq('Clean') df['Temperature'] = np.select([cond1 & cond2, cond2], [4, 3], 1)  same result
Question: I am having this issue of importing the fitz library in PyCharm. I pip installed PyMuPDF and in my code I added "import fitz" but it is giving me this error: ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/fitz/_fitz.so, 0x0002): tried: '/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/fitz/_fitz.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64')), '/System/Volumes/Preboot/Cryptexes/OS/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/fitz/_fitz.so' (no such file), '/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/fitz/_fitz.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64'))  How do I fix this error?

Answer: The problem with this not working on my mac mini m2 chip was because there wasn't any wheel that supported it but there is a new wheel. Just use this in your terminal if you're having the same problem. pip install --upgrade pymupdf