Work around strange pandas behaviour
Work around strange pandas behaviour

Here's the problem. Apparently there's a weird corner case in pandas
that occurs if you have a `DataFrame` `df` where the index on the rows
is a `DatetimeIndex`. Now, if you try to add a column named `c` to `df`
via `df.loc[:, c] = values` and the `c` is one of a few special prefixes
of the datetimes in the row index, pandas raises a `KeyError`. One of
the special prefixes that is now allowed is the year, so if, for
example, you have the date `'2011-02-02'` in the row index, trying to
add a column named `'2011'` will raise an error. What's even more grave
is the fact, that the entries of all rows, where the column name is a
prefix of the date string, get deleted, i.e. they are `NaN` after the
attempted operation. This means that the error isn't even recoverable as
the data is lost.
This is the reason for the complicated code. Since trying to just add a
column for `bus_id` threw an error, I had to generate a random column
name which doesn't already exist in the dataframe, add the column under
that name and then rename the column to `bus_id`. Also, since there is
now way to recover from the error, I couldn't just add the complicated
code in an exception handler. And since only some prefixes of date
strings aren't allowed and since checking whether a column name is a
prefix of one of the row labels is probably kinda slow, it's easier and
probably more efficient to just always do the renaming dance.

openego/eTraGo