Fix Python – Import pandas dataframe column as string not int

I would like to import the following csv as strings not as int64. Pandas read_csv automatically converts it to int64, but I need this column as string.
ID
00013007854817840016671868
00013007854817840016749251
00013007854817840016754630
00013007854817840016781876
00013007854817840017028824
00013007854817840017963235
00013007854817840018860166

df =….

Fix Python – How to convert an OrderedDict into a regular dict in python3

I am struggling with the following problem:
I want to convert an OrderedDict like this:
OrderedDict([(‘method’, ‘constant’), (‘data’, ‘1.225’)])

into a regular dict like this:
{‘method’: ‘constant’, ‘data’:1.225}

because I have to store it as string in a database. After the conversion the order is not important anymore, so I can spare the ordere….

Fix Python – NumPy or Pandas: Keeping array type as integer while having a NaN value

Is there a preferred way to keep the data type of a numpy array fixed as int (or int64 or whatever), while still having an element inside listed as numpy.NaN?
In particular, I am converting an in-house data structure to a Pandas DataFrame. In our structure, we have integer-type columns that still have NaN’s (but the dtype of the column is int). It….

Fix Python – Call int() function on every list element?

I have a list with numeric strings, like so:
numbers = [‘1’, ‘5’, ’10’, ‘8’];

I would like to convert every list element to integer, so it would look like this:
numbers = [1, 5, 10, 8];

I could do it using a loop, like so:
new_numbers = [];
for n in numbers:
new_numbers.append(int(n));
numbers = new_numbers;

Does it have to be so ugly? I’m ….

Fix Python – Checking if a string can be converted to float in Python

I’ve got some Python code that runs through a list of strings and converts them to integers or floating point numbers if possible. Doing this for integers is pretty easy
if element.isdigit():
newelement = int(element)

Floating point numbers are more difficult. Right now I’m using partition(‘.’) to split the string and checking to make sure tha….

Fix Python – Convert string to Enum in Python

What’s the correct way to convert a string to a corresponding instance of an Enum subclass? Seems like getattr(YourEnumType, str) does the job, but I’m not sure if it’s safe enough.
As an example, suppose I have an enum like
class BuildType(Enum):
debug = 200
release = 400

Given the string ‘debug’, how can I get BuildType.debug as a resul….