Change column type in pandas

You have four main options for converting types in pandas: to_numeric() – provides functionality to safely convert non-numeric types (e.g. strings) to a suitable numeric type. (See also to_datetime() and to_timedelta().) astype() – convert (almost) any type to (almost) any other type (even if it’s not necessarily sensible to do so). Also allows you to … Read more

How to convert a factor to integer\numeric without loss of information?

See the Warning section of ?factor: In particular, as.numeric applied to a factor is meaningless, and may happen by implicit coercion. To transform a factor f to approximately its original numeric values, as.numeric(levels(f))[f] is recommended and slightly more efficient than as.numeric(as.character(f)). The FAQ on R has similar advice. Why is as.numeric(levels(f))[f] more efficent than as.numeric(as.character(f))? … Read more

Get int value from enum in C#

Just cast the enum, e.g. int something = (int) Question.Role; The above will work for the vast majority of enums you see in the wild, as the default underlying type for an enum is int. However, as cecilphillip points out, enums can have different underlying types. If an enum is declared as a uint, long, … Read more

Do I cast the result of malloc?

TL;DR int *sieve = (int *) malloc(sizeof(int) * length); has two problems. The cast and that you’re using the type instead of variable as argument for sizeof. Instead, do like this: int *sieve = malloc(sizeof *sieve * length); Long version No; you don’t cast the result, since: It is unnecessary, as void * is automatically … Read more

When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?

static_cast is the first cast you should attempt to use. It does things like implicit conversions between types (such as int to float, or pointer to void*), and it can also call explicit conversion functions (or implicit ones). In many cases, explicitly stating static_cast isn’t necessary, but it’s important to note that the T(something) syntax … Read more

Why don’t Java’s +=, -=, *=, /= compound assignment operators require casting?

As always with these questions, the JLS holds the answer. In this case §15.26.2 Compound Assignment Operators. An extract: A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once. An example cited from §15.26.2 […] the following code is … Read more