Note that there are some explanatory texts on larger screens.

plurals
  1. PODataFrame.groupby(TimeGrouper="d"): Invalid length for values or for binner
    text
    copied!<p>I am trying to build groups of my data using the groupby function of pandas over a DataFrame with a DateTimeIndex. Using pd.TimeGrouper, I want to group by day.</p> <p>When I define this DataFrame, the below operation <code>n.groupby(pd.TimeGrouper("d"))</code> does not work.</p> <pre><code>n = pd.DataFrame( {"value": [5462,5462,3185]}, index=[pd.to_datetime("2013-10-13 19:03:54"), pd.to_datetime("2013-10-12 19:03:54"), pd.to_datetime("2013-10-11 13:19:23")]) </code></pre> <p>Error:</p> <pre><code>n.groupby(pd.TimeGrouper("d")) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) &lt;ipython-input-248-120eaa65b064&gt; in &lt;module&gt;() ----&gt; 1 n.groupby(pd.TimeGrouper("d")) \lib\site-packages\pandas\core\generic.pyc in groupby(self, by, axis, level, as_index, sort, group_keys, squeeze) 184 return groupby(self, by, axis=axis, level=level, as_index=as_index, 185 sort=sort, group_keys=group_keys, --&gt; 186 squeeze=squeeze) 187 188 def asfreq(self, freq, method=None, how=None, normalize=False): \lib\site-packages\pandas\core\groupby.pyc in groupby(obj, by, **kwds) 531 raise TypeError('invalid type: %s' % type(obj)) 532 --&gt; 533 return klass(obj, by, **kwds) 534 535 \lib\site-packages\pandas\core\groupby.pyc in __init__(self, obj, keys, axis, level, grouper, exclusions, selection, as_index, sort, group_keys, squeeze) 195 if grouper is None: 196 grouper, exclusions = _get_grouper(obj, keys, axis=axis, --&gt; 197 level=level, sort=sort) 198 199 self.grouper = grouper \lib\site-packages\pandas\core\groupby.pyc in _get_grouper(obj, key, axis, level, sort) 1268 1269 if isinstance(key, CustomGrouper): -&gt; 1270 gpr = key.get_grouper(obj) 1271 return gpr, [] 1272 elif isinstance(key, Grouper): \lib\site-packages\pandas\tseries\resample.pyc in get_grouper(self, obj) 106 def get_grouper(self, obj): 107 # Only return grouper --&gt; 108 return self._get_time_grouper(obj)[1] 109 110 def _get_time_grouper(self, obj): \lib\site-packages\pandas\tseries\resample.pyc in _get_time_grouper(self, obj) 112 113 if self.kind is None or self.kind == 'timestamp': --&gt; 114 binner, bins, binlabels = self._get_time_bins(axis) 115 else: 116 binner, bins, binlabels = self._get_time_period_bins(axis) \lib\site-packages\pandas\tseries\resample.pyc in _get_time_bins(self, axis) 146 147 # general version, knowing nothing about relative frequencies --&gt; 148 bins = lib.generate_bins_dt64(ax_values, bin_edges, self.closed) 149 150 if self.closed == 'right': \lib\site-packages\pandas\lib.pyd in pandas.lib.generate_bins_dt64 (pandas\lib.c:16139)() ValueError: Invalid length for values or for binner </code></pre> <p>Surprisingly, when I define the DataFrame like below, it works just fine. Notice that I changed the last day to be 2013-10-12 instead of 2013-10-11.</p> <pre><code>n = pd.DataFrame( {"value": [5462,5462,3185]}, index=[pd.to_datetime("2013-10-13 19:03:54"), pd.to_datetime("2013-10-13 19:03:54"), pd.to_datetime("2013-10-12 13:19:23")]) </code></pre> <p>In this case I get a correct group object:</p> <pre><code>n.groupby(pd.TimeGrouper("d")) &lt;pandas.core.groupby.DataFrameGroupBy object at 0x000000000A3D84E0&gt; </code></pre> <p>I already looked up some of the core functions of pandas in the source code, but I am not sure whether this is a bug or whether I just do not know how to use the function properly.</p> <p>Notice also that aggregating on month works just fine.</p> <p>Thank you for your help.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload