In the following piece of code, I group the points of a DataFrame by their X value into bins. Now I want to assign a group ID to the Y column, but pandas keep throwing me a warning of type SettingWithCopyWarning
. What am I doing wrong?
import numpy as np
import pandas as pd
d = np.random.random((10, 2))
d[:, 1] = 0
m = pd.DataFrame(d, columns=("x", "gid"))
dx = 0.2
grp = m.groupby(lambda i: int(m["x"][i] / dx))
gid = 1
for name, group in grp:
group["gid"][:] = gid # This line crashes!
gid += 1
print(m)
Here is the warning thrown:
/usr/lib/python3.4/site-packages/pandas/core/series.py:677: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self._set_with(key, value)
sys:1: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy