Get the date histogram aggregations.

        :param dataframe: if true, return a pandas.DataFrame object
182998
def timeseries(self, dataframe=False):
        

        self.query.by_period()
        ts = super().timeseries(dataframe=dataframe)
        ts['value'] = ts['value'].apply(lambda x: float("%.2f" % x))
        return ts
402495
def get_timeseries(self, *args, **kwargs):
        
        import predix.data.timeseries
        ts = predix.data.timeseries.TimeSeries(*args, **kwargs)
        return ts
804891
def _to_df(self, query, conn, index_col=None, coerce_float=True, params=None,
               parse_dates=None, columns=None):
        
        return pd.io.sql.read_sql(str(query), conn, index_col=index_col,
                                  coerce_float=coerce_float, params=params,
                                  parse_dates=parse_dates, columns=columns)
79441
def agg(self, aggregations):
        
        check_type(aggregations, list)

        new_index = Index(np.array(aggregations, dtype=np.bytes_), np.dtype(np.bytes_))

        return _series_agg(self, aggregations, new_index)
64825
def aggregations(self):
        """"""

        prev_month_start = get_prev_month(self.end, self.query.interval_)
        self.query.since(prev_month_start)
        agg = super().aggregations()
        if agg is None:
            agg = 0  # None is because NaN in ES. Let's convert to 0
        return agg
804375
def mean_date(dt_list):
    
    dt_list_sort = sorted(dt_list)
    dt_list_sort_rel = [dt - dt_list_sort[0] for dt in dt_list_sort]
    avg_timedelta = sum(dt_list_sort_rel, timedelta())/len(dt_list_sort_rel)
    return dt_list_sort[0] + avg_timedelta
980437
def _get_group_dataframes(self):
        
        if isinstance(self.data, GroupedDataFrame):
            grouper = self.data.groupby()
            # groupby on categorical columns uses the categories
            # even if they are not present in the data. This
            # leads to empty groups. We exclude them.
            return (gdf for _, gdf in grouper if not gdf.empty)
        else:
            return (self.data, )
841319
def histogram_hour_counts(
    df,
    variable
    ):
    
    if not df.index.dtype in ["datetime64[ns]", "<M8[ns]", ">M8[ns]"]:
        log.error("index is not datetime")
        return False
    counts = df.groupby(df.index.hour)[variable].count()
    counts.plot(kind = "bar", width = 1, rot = 0, alpha = 0.7)
167889
def rolling(self, *args, **kwargs):
        
        from pandas.core.window import RollingGroupby
        return RollingGroupby(self, *args, **kwargs)
706192
def expand_dates(df, columns=[]):
    
    columns = df.columns.intersection(columns)
    df2 = df.reindex(columns=set(df.columns).difference(columns))
    for column in columns:
        df2[column + '_year'] = df[column].apply(lambda x: x.year)
        df2[column + '_month'] = df[column].apply(lambda x: x.month)
        df2[column + '_day'] = df[column].apply(lambda x: x.day)
    return df2
154698
def _prep_pandas_groupby(self):
        """"""
        myargs = self._myargs
        mykwargs = self._mykwargs

        def extract_keys(groupedFrame):
            for key, group in groupedFrame:
                yield (key, group)

        def group_and_extract(frame):
            return extract_keys(frame.groupby(*myargs, **mykwargs))

        self._baseRDD = self._prdd._rdd()
        self._distributedRDD = self._baseRDD.flatMap(group_and_extract)
        self._mergedRDD = self._sortIfNeeded(
            self._group(self._distributedRDD))
1038644
def _fetch_dataframe(self):
        
        def reshape(training_summary):
            # Helper method to reshape a single training job summary into a dataframe record
            out = {}
            for k, v in training_summary['TunedHyperParameters'].items():
                # Something (bokeh?) gets confused with ints so convert to float
                try:
                    v = float(v)
                except (TypeError, ValueError):
                    pass
                out[k] = v
            out['TrainingJobName'] = training_summary['TrainingJobName']
            out['TrainingJobStatus'] = training_summary['TrainingJobStatus']
            out['FinalObjectiveValue'] = training_summary.get('FinalHyperParameterTuningJobObjectiveMetric',
                                                              {}).get('Value')

            start_time = training_summary.get('TrainingStartTime', None)
            end_time = training_summary.get('TrainingEndTime', None)
            out['TrainingStartTime'] = start_time
            out['TrainingEndTime'] = end_time
            if start_time and end_time:
                out['TrainingElapsedTimeSeconds'] = (end_time - start_time).total_seconds()
            return out
        # Run that helper over all the summaries.
        df = pd.DataFrame([reshape(tjs) for tjs in self.training_job_summaries()])
        return df
421535
def to_pandas(self):
        
        dataframe = self.get().to_pandas()
        assert type(dataframe) is pandas.DataFrame or type(dataframe) is pandas.Series

        return dataframe
39842
def started_after(self, date):
        '''
        
        '''
        dt = Timestamp(date)
        starts = self.groupby(self._oid).apply(lambda df: df._start.min())
        oids = set(starts[starts > dt].index.tolist())
        return self[self._oid.apply(lambda v: v in oids)]
308029
def fetch_csv_dataframe(
        download_url,
        filename=None,
        subdir=None,
        **pandas_kwargs):
    
    path = fetch_file(
        download_url=download_url,
        filename=filename,
        decompress=True,
        subdir=subdir)
    return pd.read_csv(path, **pandas_kwargs)
857988
def _get_wr(cls, df, n_days):
        
        n_days = int(n_days)
        ln = df['low'].rolling(min_periods=1, window=n_days,
                               center=False).min()

        hn = df['high'].rolling(min_periods=1, window=n_days,
                                center=False).max()
        column_name = 'wr_{}'.format(n_days)
        df[column_name] = (hn - df['close']) / (hn - ln) * 100
769069
def _convert(frame):
    
    frame = frame.convert_objects(convert_numeric=True)
    for column in frame:
        if column in c.dates:
            frame[column] = frame[column].astype('datetime64')
    return frame