I have a dictionary annotation The value of time: will always be formatted like 2022-01-01 00:00:00, or “%Y-%m-%d %H:%M:%S”. I’d like a way to express this in the type annotation Something like WIth the goal of IDE hinting through VSCode Intellisense and Pylance. Are regex-defined type annotations supported? Answer Leaving out philosophical discussions about what should or should not be
Tag: types
How to take the item from string and use it as a value
I have a string and I need to use this string to fit a model. However when I try try to do this, of course it raises an error which can be seen below. How can I use this string to fit the model? Answer You need to evaluate the string into a Python object, ie do See documentation of
Type annotation for partial functions
we have multiple partial-like functions with same type annotation with args and kwargs like: can I somehow create a template for functions apple, orange, banana and assign it to them? I thought about Protocol with __call__ definition, but it is unclear how to assign it to functions Answer Rather than a function, you can use functools.partial: Another possibility would be
Python type hint for Iterable[str] that isn’t str
In Python, is there a way to distinguish between strings and other iterables of strings? A str is valid as an Iterable[str] type, but that may not be the correct input for a function. For example, in this trivial example that is intended to operate on sequences of filenames: Passing in a single filename would produce the wrong result but
Sort dict by multiple keys and with int and None data in python
so I have a dict : I want to sort my dict on ‘i.chromosome’, ‘i.linkageGroup’ and ‘i.positionCm’ What I am doing : And i got : In python 2, they were doing : Could you help me please, I am lost ! Answer Given and the requirement that Nones are for int, you can solve it using where I’ve made
Mypy: incompatible type error during set update
Mypy returns an error if the set is updated with new tuple using add() code.py error body As far as I know, it is common practice to add new tuplets to the set. The add() method can add a tuple object as an element in the set Why does mypy think it’s not allowed? Answer adgroups_by_campaign_id is marked as Dict[CampaignId,
TypeError: cannot concatenate object of type ”; only Series and DataFrame objs are valid
I have a list of 10 dataframes named d0, d1, d2,…d9. All have 3 columns and 100 rows. I want to merge all dataframes so that I can have 3 columns and 1000 rows and then convert it into an array. The above code throws error: I used the solution suggested in pd.concat in pandas is giving a TypeError: cannot
Multiple data types in a python list
I have a list for birth data each record has 3 columns for [DOB, weight, height] as such: I need to change the data types of this as they are all strings in the list I want the first item in the record to be datetime and then the rest to be floats. I have: I get an attribute error:
How to convert ‘+’ into + in Python
I’m willing to make a program that evaluates all possible combinations of operations ( + , – , * , / ) on a set of positive integers of length 6 (eg : [1, 6, 3, 9, 2, 9] ). To do so, I am using the list and wrote a nested loop to create all possibilities by calling each
Python: more elegant way to assert type of function input
I am a complete beginner in Python… Currently I am trying to write a function which first checks whether the inputs x and y are int or float. My guess which does the job is if (type(x) != int and type(x) != float) or (type(y) != int and type(y) != float): However, this seems quite clumsy / inefficient to me