Python pandas.to_datetime() Examples

The following are code examples for showing how to use pandas.to_datetime(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: synthetic-data-tutorial   Author: theodi   File: deidentify.py    MIT License 7 votes vote down vote up
def put_time_in_4_hour_bins(hospital_ae_df: pd.DataFrame) -> pd.DataFrame:
    """ 
    Gives each hospital a random integer number and adds a new column
    with these numbers. Drops the hospital name column. 

    Keyword arguments:
    hospital_ae_df -- Hopsitals A&E records dataframe
    """

    arrival_times = pd.to_datetime(hospital_ae_df['Arrival Time'])
    hospital_ae_df['Arrival Date'] = arrival_times.dt.strftime('%Y-%m-%d')
    hospital_ae_df['Arrival Hour'] = arrival_times.dt.hour

    hospital_ae_df['Arrival hour range'] = pd.cut(
        hospital_ae_df['Arrival Hour'], 
        bins=[0, 4, 8, 12, 16, 20, 24], 
        labels=['00-03', '04-07', '08-11', '12-15', '16-19', '20-23'], 
        include_lowest=True
    )
    hospital_ae_df = hospital_ae_df.drop('Arrival Time', 1)
    hospital_ae_df = hospital_ae_df.drop('Arrival Hour', 1)

    return hospital_ae_df 
Example 2
Project: aospy   Author: spencerahill   File: test_utils_times.py    Apache License 2.0 6 votes vote down vote up
def test_monthly_mean_at_each_ind():
    times_submonthly = pd.to_datetime(['2000-06-01', '2000-06-15',
                                       '2000-07-04', '2000-07-19'])
    times_means = pd.to_datetime(['2000-06-01', '2000-07-01'])
    len_other_dim = 2
    arr_submonthly = xr.DataArray(
        np.random.random((len(times_submonthly), len_other_dim)),
        dims=[TIME_STR, 'dim0'], coords={TIME_STR: times_submonthly}
    )
    arr_means = xr.DataArray(
        np.random.random((len(times_means), len_other_dim)),
        dims=arr_submonthly.dims, coords={TIME_STR: times_means}
    )
    actual = monthly_mean_at_each_ind(arr_means, arr_submonthly)
    desired_values = np.stack([arr_means.values[0]] * len_other_dim +
                              [arr_means.values[1]] * len_other_dim,
                              axis=0)
    desired = xr.DataArray(desired_values, dims=arr_submonthly.dims,
                           coords=arr_submonthly.coords)
    assert actual.identical(desired) 
Example 3
Project: aospy   Author: spencerahill   File: test_utils_times.py    Apache License 2.0 6 votes vote down vote up
def test_yearly_average_no_mask():
    times = pd.to_datetime(['2000-06-01', '2000-06-15',
                            '2001-07-04', '2001-10-01', '2001-12-31',
                            '2004-01-01'])
    arr = xr.DataArray(np.random.random((len(times),)),
                       dims=[TIME_STR], coords={TIME_STR: times})
    dt = arr.copy(deep=True)
    dt.values = np.random.random((len(times),))

    actual = yearly_average(arr, dt)

    yr2000 = (arr[0]*dt[0] + arr[1]*dt[1]) / (dt[0] + dt[1])
    yr2001 = ((arr[2]*dt[2] + arr[3]*dt[3] + arr[4]*dt[4]) /
              (dt[2] + dt[3] + dt[4]))
    yr2004 = arr[-1]
    yrs_coord = [2000, 2001, 2004]
    yr_avgs = np.array([yr2000, yr2001, yr2004])
    desired = xr.DataArray(yr_avgs, dims=['year'], coords={'year': yrs_coord})
    xr.testing.assert_allclose(actual, desired) 
Example 4
Project: gamry-parser   Author: bcliang   File: chronoa.py    MIT License 6 votes vote down vote up
def load(self, filename=None):
        """ run the parser to load the experimental data from file

        Args:
            None

        Returns:
            None

        """

        super().load(filename)
        if self.to_timestamp:
            "we want data returned with timestamps instead of relative time"
            start_time = pd.to_datetime(self.header['DATE'] + ' ' + self.header['TIME'])  # start time
            for curve in self.curves:
                curve['T'] = (start_time + pd.to_timedelta(curve['T'], 's')) 
Example 5
Project: GreenGuard   Author: D3-AI   File: data.py    MIT License 6 votes vote down vote up
def _filter_by_timestamp(data, target_times):
    LOGGER.info('Parsing timestamps')
    timestamps = pd.to_datetime(data['timestamp'], format='%m/%d/%y %H:%M:%S')
    data['timestamp'] = timestamps

    LOGGER.info('Filtering by timestamp')

    related = [False] * len(timestamps)
    for row in target_times.itertuples():
        related |= (row.start <= timestamps) & (timestamps <= row.end)

    data = data[related]

    LOGGER.info('Selected %s readings by timestamp', len(data))

    return data 
Example 6
Project: GreenGuard   Author: D3-AI   File: csv.py    MIT License 6 votes vote down vote up
def __filter_by_timestamp(self, readings, timestamps):
        LOGGER.debug('Parsing timestamps')
        readings_ts = pd.to_datetime(readings['timestamp'], format='%m/%d/%y %H:%M:%S')
        readings['timestamp'] = readings_ts

        LOGGER.debug('Filtering by timestamp')

        related = [False] * len(readings)
        for row in timestamps.itertuples():
            lower = row.start <= readings_ts
            upper = readings_ts <= row.stop
            related |= lower & upper

        readings = readings[related]

        LOGGER.debug('Selected %s readings by timestamp', len(readings))

        return readings.copy() 
Example 7
Project: GreenGuard   Author: D3-AI   File: csv.py    MIT License 6 votes vote down vote up
def load(self, target_times, window_size, signals=None, debug=False):
        if isinstance(target_times, str):
            target_times = pd.read_csv(target_times)
            target_times['cutoff_time'] = pd.to_datetime(target_times['cutoff_time'])

        if isinstance(signals, pd.DataFrame):
            signals = signals.signal_id

        window_size = pd.to_timedelta(window_size)
        timestamps = self._get_timestamps(target_times, window_size)

        readings = list()
        for turbine_id in timestamps.turbine_id.unique():
            readings.append(self._load_turbine(turbine_id, timestamps, signals))

        dask_scheduler = 'single-threaded' if debug else None
        computed = dask.compute(*readings, scheduler=dask_scheduler)
        readings = pd.concat((c for c in computed if len(c)), ignore_index=True, sort=False)

        LOGGER.info('Loaded %s turbine readings', len(readings))

        return readings 
Example 8
Project: pymapd-examples   Author: omnisci   File: OKR_techsup_discourse.py    Apache License 2.0 6 votes vote down vote up
def main():
    dfcreds = get_credentials(keyfile)
    str_authentication = "&api_key=" + dfcreds['access_token'] + "&api_username=" + dfcreds['api_username']
    for url, fn in endpoints:
        url_get = url + str_authentication
        df = pd.read_json(url_get, orient="columns")
    #isolate the list
        cell = df.iloc[3,0]
    #format and clean up the data
        df = pd.DataFrame.from_dict(cell) #turn the list into a dataframe
        dfnew = pd.DataFrame(df, columns=["c1_timestamp","c2_value"]) # set the column names
        dfnew["c1_timestamp"] = pd.to_datetime(df["x"])
        dfnew["c2_value"] = pd.to_numeric(df["y"])
    # write to csv
        print ("writing csv to " + fn)
        dfnew.to_csv(fn, index=False, date_format="%Y-%m-%d") # write to csv 
Example 9
Project: qdata   Author: X0Leon   File: fetch.py    MIT License 6 votes vote down vote up
def get_ticks_tushare(symbol, date, retry_count=3, pause=0.001, drop_auction=True):
    """
    使用tushare(来自sina数据源)库获取tick数据
    参数
    - symbol: 股票或指数的代码,如'600008'
    - date: 日期格式,'YYYY-MM-DD'
    - retry_count: 如遇网络等问题重复执行的次数
    - pause: 重复请求数据过程中暂停的秒数
    - drop_auction: 是否剔除竞价数据
    """
    df = ts.get_tick_data(code=symbol, date=date, retry_count=retry_count, pause=pause)
    if len(df) < 4:
        return None
    else:
        df['time'] = date + ' ' + df['time']
        df['time'] = pd.to_datetime(df['time'], format='%Y-%m-%d %H:%M:%S')
        df = df.set_index('time')
        df.index.name = 'datetime'
        if drop_auction:
            return df.sort_index()[1:]
        else:
            return df.sort_index() 
Example 10
Project: xalpha   Author: refraction-ray   File: info.py    MIT License 6 votes vote down vote up
def _fetch_csv(self, path):
        """
        fetch the information and pricetable from path+code.csv, not recommend to use manually,
        just set the fetch label to be true when init the object

        :param path:  string of folder path
        """
        try:
            content = pd.read_csv(path + self.code + ".csv")
            pricetable = content.iloc[1:]
            datel = list(pd.to_datetime(pricetable.date))
            self.price = pricetable[["netvalue", "totvalue", "comment"]]
            self.price["date"] = datel
            saveinfo = json.loads(content.iloc[0].date)
            if not isinstance(saveinfo, dict):
                raise FundTypeError("This csv doesn't looks like from fundinfo")
            self.segment = saveinfo["segment"]
            self.feeinfo = saveinfo["feeinfo"]
            self.name = saveinfo["name"]
            self.rate = saveinfo["rate"]
        except FileNotFoundError as e:
            # print('no saved copy of fund %s' % self.code)
            raise e 
Example 11
Project: xalpha   Author: refraction-ray   File: info.py    MIT License 6 votes vote down vote up
def _fetch_csv(self, path):
        """
        fetch the information and pricetable from path+code.csv, not recommend to use manually,
        just set the fetch label to be true when init the object

        :param path:  string of folder path
        """
        try:
            pricetable = pd.read_csv(path + self.code + ".csv")
            datel = list(pd.to_datetime(pricetable.date))
            self.price = pricetable[["netvalue", "totvalue", "comment"]]
            self.price["date"] = datel

        except FileNotFoundError as e:
            # print('no saved copy of %s' % self.code)
            raise e 
Example 12
Project: xalpha   Author: refraction-ray   File: info.py    MIT License 6 votes vote down vote up
def _fetch_csv(self, path):
        """
        fetch the information and pricetable from path+code.csv, not recommend to use manually,
        just set the fetch label to be true when init the object

        :param path:  string of folder path
        """
        try:
            content = pd.read_csv(path + self.code + ".csv")
            pricetable = content.iloc[1:]
            datel = list(pd.to_datetime(pricetable.date))
            self.price = pricetable[["netvalue", "totvalue", "comment"]]
            self.price["date"] = datel
            self.name = content.iloc[0].comment
        except FileNotFoundError as e:
            # print('no saved copy of %s' % self.code)
            raise e 
Example 13
Project: GeoPy   Author: aerler   File: bc_methods.py    GNU General Public License v3.0 6 votes vote down vote up
def correctionByTime(self, varname, time, ldt=True, time_idx=0, **kwargs):
        ''' return formatted correction arrays based on an array or list of datetime objects '''
        # interprete datetime: convert to monthly indices
        if ldt:
            # if the time array/list is datetime-like, we have to extract the month
            if isinstance(time,(list,tuple)):
                time = [pd.to_datetime(t).month-1 for t in time]
            elif isinstance(time,np.ndarray):
                if np.issubdtype(time.dtype, np.datetime64):
                    time = time.astype('datetime64[M]').astype('int16') % 12
            else:
                raise TypeError(time)
        # ... but in the end we need an array of indices
        time = np.asarray(time, dtype='int16')
        # construct view into correction array based on indices
        correction = np.take(self._correction[varname], time, axis=time_idx,)
        # return correction factors for requested time indices
        return correction 
Example 14
Project: GeoPy   Author: aerler   File: SnoDAS.py    GNU General Public License v3.0 6 votes vote down vote up
def getFilenameFolder(varname=None, date=None, root_folder=root_folder, lgzip=True):
    ''' simple function to generate the filename and folder of a file based on variable and date '''
    if not isinstance(date,dt.datetime):
        date = pd.to_datetime(date)
    datestr = '{:04d}{:02d}{:02d}'.format(date.year,date.month,date.day)
    varatts = binary_varatts[varname]
    # construct filename
    if varatts['variable_type'].lower() == 'driving': var_code = 'v0' + varatts['product_code'] + 'S'
    else: var_code = 'v1' + varatts['product_code']
    t_code = varatts['t_code']
    if varatts['product_code'] == '1044' and date < dt.datetime(2010,2,17): t_code = 'T0001'
    agg_code = varatts['v_code'] + t_code
    offset_code = 'P000' if varatts['variable_type'].lower() == 'diagnostic' else 'P001'
    I_code = 'H' if t_code.upper() == 'T0001' else 'D'
    filename = 'zz_ssm{:s}{:s}TTNATS{:s}05{:s}{:s}.dat'.format(var_code,agg_code,datestr,I_code,offset_code)        
    if lgzip: filename += '.gz'
    # contruct folder
    mon = '{:02d}_{:s}'.format(date.month,name_of_month[date.month-1][:3].title())
    folder = '{:s}/data/{:04d}/{:s}/SNODAS_unmasked_{:s}/'.format(root_folder,date.year,mon,datestr)
    return folder,filename 
Example 15
Project: thewarden   Author: pxsocs   File: routes.py    MIT License 6 votes vote down vote up
def tradedetails():
    if request.method == "GET":
        id = request.args.get("id")
        # if tradesonly is true then only look for buy and sells
        tradesonly = request.args.get("trades")
        df = pd.read_sql_table("trades", db.engine)
        # Filter only the trades for current user
        df = df[(df.user_id == current_user.username)]
        df = df[(df.trade_reference_id == id)]
        # Filter only buy and sells, ignore deposit / withdraw
        if tradesonly:
            df = df[(df.trade_operation == "B") | (df.trade_operation == "S")]
        # df['trade_date'] = pd.to_datetime(df['trade_date'])
        df.set_index("trade_reference_id", inplace=True)
        df.drop("user_id", axis=1, inplace=True)
        details = df.to_json()
        return details 
Example 16
Project: Aegis   Author: jlillywh   File: time_series.py    GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, values_name='values', start_date='1/1/19', periods=365):
        """Initialize with values of zero for the duration and frequency.
        
            Parameters
            ----------
            date_range : pandas DatetimeIndex
            Represents the date range for the time series
            Must be used to provide the index list for the values"""
        _start_date = datetime.strptime(start_date, '%x').date()
        self.date_rng = pd.date_range(start=start_date, periods=periods)
        self.values_name = values_name
        # self.df = pd.DataFrame({date': [_start_date + timedelta(days=x) for x in range(periods)], self.values_name: pd.Series(np.random.randn(periods))})
        # self.df = self.df.set_index('date')
        self.df = pd.DataFrame(self.date_rng, columns=['date'])
        self.df[values_name] = np.random.randint(0, 100, size=len(self.date_rng))
        self.df['datetime'] = pd.to_datetime(self.df['date'])
        self.df = self.df.set_index('datetime')
        self.df.drop(['date'], axis=1, inplace=True)
        
        # self.date_range = pd.date_range(start_date, periods=periods, freq=freq)
        # np.random.seed(seed=1111)
        # self.data = np.random.randint(1, high=100, size=len(self.date_range))
        # self.df = pd.DataFrame({'Date': self.date_range, 'Values': self.data})
        # self.df.set_index('Date') 
Example 17
Project: recordlinkage   Author: J535D165   File: test_compare.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_dates(self):

        A = DataFrame({
            'col':
            to_datetime(
                ['2005/11/23', nan, '2004/11/23', '2010/01/10', '2010/10/30'])
        })
        B = DataFrame({
            'col':
            to_datetime([
                '2005/11/23', '2010/12/31', '2005/11/23', '2010/10/01',
                '2010/9/30'
            ])
        })
        ix = MultiIndex.from_arrays([A.index.values, B.index.values])

        comp = recordlinkage.Compare()
        comp.date('col', 'col')
        result = comp.compute(ix, A, B)[0]

        expected = Series([1, 0, 0, 0.5, 0.5], index=ix, name=0)

        pdt.assert_series_equal(result, expected) 
Example 18
Project: recordlinkage   Author: J535D165   File: test_compare.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_date_incorrect_dtype(self):

        A = DataFrame({
            'col':
            ['2005/11/23', nan, '2004/11/23', '2010/01/10', '2010/10/30']
        })
        B = DataFrame({
            'col': [
                '2005/11/23', '2010/12/31', '2005/11/23', '2010/10/01',
                '2010/9/30'
            ]
        })
        ix = MultiIndex.from_arrays([A.index.values, B.index.values])

        A['col1'] = to_datetime(A['col'])
        B['col1'] = to_datetime(B['col'])

        comp = recordlinkage.Compare()
        comp.date('col', 'col1')
        pytest.raises(ValueError, comp.compute, ix, A, B)

        comp = recordlinkage.Compare()
        comp.date('col1', 'col')
        pytest.raises(ValueError, comp.compute, ix, A, B) 
Example 19
Project: dinoSARaws   Author: scottyhq   File: get_inventory_asf.py    MIT License 6 votes vote down vote up
def load_asf_json(jsonfile):
    ''' Convert JSON metadata from asf query to dataframe '''
    with open(jsonfile) as f:
        meta = json.load(f)[0] #list of scene dictionaries

    df = pd.DataFrame(meta)
    polygons = df.stringFootprint.apply(shapely.wkt.loads)
    gf = gpd.GeoDataFrame(df,
                          crs={'init': 'epsg:4326'},
                          geometry=polygons)

    gf['timeStamp'] = pd.to_datetime(gf.sceneDate, format='%Y-%m-%d %H:%M:%S')
    gf['sceneDateString'] = gf.timeStamp.apply(lambda x: x.strftime('%Y-%m-%d'))
    gf['dateStamp'] = pd.to_datetime(gf.sceneDateString)
    gf['utc'] = gf.timeStamp.apply(lambda x: x.strftime('%H:%M:%S'))
    gf['orbitCode'] = gf.relativeOrbit.astype('category').cat.codes

    return gf 
Example 20
Project: pyplis   Author: jgliss   File: imagelists.py    GNU General Public License v3.0 6 votes vote down vote up
def get_img_meta_all(self):
        """Load all available meta data from fits files.

        Returns
        -------
        dataFrame
            containing all metadata

        """
        if self.fitsfiles == []:
            logger.warning("ImgListLayered was intialised without providing the "
                  "fitsfile (e.g. only by meta file). self.get_img_meta_all "
                  "will return the existing metaData.")
            return self.metaData

        # Exatract information of every image in every fits file in fitsFile
        meta_single = [self.get_img_meta_one_fitsfile(file_path)
                       for file_path in self.fitsfiles]
        meta = concat(meta_single)
        meta['img_id'] = arange(0, len(meta), 1)
        meta.index = to_datetime(meta.index)
        return meta 
Example 21
Project: pysat   Author: pysat   File: test_files.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_year_doy_files_direct_call_to_from_os(self):
        # create a bunch of files by year and doy
        start = pysat.datetime(2008, 1, 1)
        stop = pysat.datetime(2009, 12, 31)
        create_files(self.testInst, start, stop, freq='1D')
        # use from_os function to get pandas Series of files and dates
        files = pysat.Files.from_os(data_path=self.testInst.files.data_path,
                                    format_str=''.join(('pysat_testing_junk_',
                                                        '{year:04d}_gold_',
                                                        '{day:03d}_stuff.',
                                                        'pysat_testing_file')))
        # check overall length
        check1 = len(files) == (365 + 366)
        # check specific dates
        check2 = pds.to_datetime(files.index[0]) == pysat.datetime(2008, 1, 1)
        check3 = pds.to_datetime(files.index[365]) == \
            pysat.datetime(2008, 12, 31)
        check4 = pds.to_datetime(files.index[-1]) == \
            pysat.datetime(2009, 12, 31)
        assert(check1 & check2 & check3 & check4) 
Example 22
Project: pysat   Author: pysat   File: test_files.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_year_doy_files_no_gap_in_name_direct_call_to_from_os(self):
        # create a bunch of files by year and doy
        start = pysat.datetime(2008, 1, 1)
        stop = pysat.datetime(2009, 12, 31)
        create_files(self.testInst, start, stop, freq='1D',
                     root_fname=''.join(('pysat_testing_junk_{year:04d}',
                                         '{day:03d}_stuff.pysat_testing_',
                                         'file')))
        # use from_os function to get pandas Series of files and dates
        files = pysat.Files.from_os(data_path=self.testInst.files.data_path,
                                    format_str=''.join(('pysat_testing_junk_',
                                                        '{year:04d}{day:03d}_',
                                                        'stuff.pysat_testing_',
                                                        'file')))
        # check overall length
        check1 = len(files) == (365 + 366)
        # check specific dates
        check2 = pds.to_datetime(files.index[0]) == pysat.datetime(2008, 1, 1)
        check3 = pds.to_datetime(files.index[365]) == \
            pysat.datetime(2008, 12, 31)
        check4 = pds.to_datetime(files.index[-1]) == \
            pysat.datetime(2009, 12, 31)
        assert(check1 & check2 & check3 & check4) 
Example 23
Project: pysat   Author: pysat   File: test_files.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_year_month_day_files_direct_call_to_from_os(self):
        # create a bunch of files by year and doy
        start = pysat.datetime(2008, 1, 1)
        stop = pysat.datetime(2009, 12, 31)
        create_files(self.testInst, start, stop, freq='1D', use_doy=False,
                     root_fname=''.join(('pysat_testing_junk_{year:04d}_gold_',
                                         '{day:03d}_stuff_{month:02d}.pysat_',
                                         'testing_file')))
        # use from_os function to get pandas Series of files and dates
        files = pysat.Files.from_os(data_path=self.testInst.files.data_path,
                                    format_str=''.join(('pysat_testing_junk_',
                                                        '{year:04d}_gold_',
                                                        '{day:03d}_stuff_',
                                                        '{month:02d}.pysat_',
                                                        'testing_file')))
        # check overall length
        check1 = len(files) == (365 + 366)
        # check specific dates
        check2 = pds.to_datetime(files.index[0]) == pysat.datetime(2008, 1, 1)
        check3 = pds.to_datetime(files.index[365]) == \
            pysat.datetime(2008, 12, 31)
        check4 = pds.to_datetime(files.index[-1]) == \
            pysat.datetime(2009, 12, 31)
        assert(check1 & check2 & check3 & check4) 
Example 24
Project: pysat   Author: pysat   File: test_files.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_year_month_day_hour_minute_files_direct_call_to_from_os(self):
        root_fname = ''.join(('pysat_testing_junk_{year:04d}_gold_{day:03d}_',
                              'stuff_{month:02d}_{hour:02d}{minute:02d}.',
                              'pysat_testing_file'))
        # create a bunch of files by year and doy
        start = pysat.datetime(2008, 1, 1)
        stop = pysat.datetime(2008, 1, 4)
        create_files(self.testInst, start, stop, freq='30min',
                     use_doy=False,
                     root_fname=root_fname)
        # use from_os function to get pandas Series of files and dates
        files = pysat.Files.from_os(data_path=self.testInst.files.data_path,
                                    format_str=root_fname)
        # check overall length
        check1 = len(files) == 145
        # check specific dates
        check2 = pds.to_datetime(files.index[0]) == pysat.datetime(2008, 1, 1)
        check3 = pds.to_datetime(files.index[1]) == \
            pysat.datetime(2008, 1, 1, 0, 30)
        check4 = pds.to_datetime(files.index[10]) == \
            pysat.datetime(2008, 1, 1, 5, 0)
        check5 = pds.to_datetime(files.index[-1]) == pysat.datetime(2008, 1, 4)
        assert(check1 & check2 & check3 & check4 & check5) 
Example 25
Project: pysat   Author: pysat   File: test_files.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_year_month_files_direct_call_to_from_os(self):
        # create a bunch of files by year and doy
        start = pysat.datetime(2008, 1, 1)
        stop = pysat.datetime(2009, 12, 31)
        create_files(self.testInst, start, stop, freq='1MS',
                     root_fname=''.join(('pysat_testing_junk_{year:04d}_gold_',
                                         'stuff_{month:02d}.pysat_testing_',
                                         'file')))
        # use from_os function to get pandas Series of files and dates
        files = pysat.Files.from_os(data_path=self.testInst.files.data_path,
                                    format_str=''.join(('pysat_testing_junk_',
                                                        '{year:04d}_gold_',
                                                        'stuff_{month:02d}.',
                                                        'pysat_testing_file')))
        # check overall length
        check1 = len(files) == 24
        # check specific dates
        check2 = pds.to_datetime(files.index[0]) == pysat.datetime(2008, 1, 1)
        check3 = pds.to_datetime(files.index[11]) == \
            pysat.datetime(2008, 12, 1)
        check4 = pds.to_datetime(files.index[-1]) == \
            pysat.datetime(2009, 12, 1)
        assert(check1 & check2 & check3 & check4) 
Example 26
Project: pycovjson   Author: Reading-eScience-Centre   File: read_netcdf.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def convert_time(self, t_variable):
        """
        Formats time objects to CovJSON compliant strings.

        :param t_variable: Time Variable
        :return: list of formatted datetime strings
        """
        date_list = []
        times = self.dataset[t_variable].values

        for time in times:
            try:
                time = pd.to_datetime(str(time))
                date_list.append(time.strftime('%Y-%m-%dT%H:%M:%SZ'))
            except ValueError as ve:
                print("Error parsing and converting '%s' variable object to CovJSON compliant string."  % (t_variable), ve)

        return date_list 
Example 27
Project: pandas_utility   Author: mmphego   File: pandas_utility.py    MIT License 6 votes vote down vote up
def col_to_datetime(df, column_name, date_format="%Y-%m-%d"):
        """
        Parameters
        ----------
        df : pandas.core.frame.DataFrame
            Two-dimensional size-mutable,
            potentially heterogeneous tabular data
        column_name : TYPE
            Column to change datetime
        date_format : str, optional
            Description

        Returns
        -------
        `pandas.core.frame.DataFrame`
            DataFrame
        """
        return pd.to_datetime(df[column_name], format=date_format) 
Example 28
Project: osqf2015   Author: mvaz   File: model.py    MIT License 5 votes vote down vote up
def compute_scenarios(self, d, n_scenarios=750):
        # identify returns
        dates = pd.to_datetime(d, unit='ms')
        max_date = dates[0].date()
        min_date = max_date.replace(year=max_date.year-3)

        logging.info('Computing returns between ') #, str(max_date), ' and ', str(min_date))
        self.returns_df = self.df[min_date:max_date].ix[-n_scenarios-1:]
        neutral, vola = self.returns_df.ix[max_date][['Close', 'Vola']]
        scenarios = neutral * np.exp( vola * self.returns_df.ix[:-1].DevolLogReturns )
        return scenarios, neutral 
Example 29
Project: aospy   Author: spencerahill   File: times.py    Apache License 2.0 5 votes vote down vote up
def apply_time_offset(time, years=0, months=0, days=0, hours=0):
    """Apply a specified offset to the given time array.

    This is useful for GFDL model output of instantaneous values.  For example,
    3 hourly data postprocessed to netCDF files spanning 1 year each will
    actually have time values that are offset by 3 hours, such that the first
    value is for 1 Jan 03:00 and the last value is 1 Jan 00:00 of the
    subsequent year.  This causes problems in xarray, e.g. when trying to group
    by month.  It is resolved by manually subtracting off those three hours,
    such that the dates span from 1 Jan 00:00 to 31 Dec 21:00 as desired.

    Parameters
    ----------
    time : xarray.DataArray representing a timeseries
    years, months, days, hours : int, optional
        The number of years, months, days, and hours, respectively, to offset
        the time array by.  Positive values move the times later.

    Returns
    -------
    pandas.DatetimeIndex

    Examples
    --------
    Case of a length-1 input time array:

    >>> times = xr.DataArray(datetime.datetime(1899, 12, 31, 21))
    >>> apply_time_offset(times)
    Timestamp('1900-01-01 00:00:00')

    Case of input time array with length greater than one:

    >>> times = xr.DataArray([datetime.datetime(1899, 12, 31, 21),
    ...                       datetime.datetime(1899, 1, 31, 21)])
    >>> apply_time_offset(times) # doctest: +NORMALIZE_WHITESPACE
    DatetimeIndex(['1900-01-01', '1899-02-01'], dtype='datetime64[ns]',
                  freq=None)
    """
    return (pd.to_datetime(time.values) +
            pd.DateOffset(years=years, months=months, days=days, hours=hours)) 
Example 30
Project: aospy   Author: spencerahill   File: test_utils_times.py    Apache License 2.0 5 votes vote down vote up
def test_apply_time_offset():
    start = datetime.datetime(1900, 5, 10)
    years, months, days, hours = -2, 1, 7, 3
    # test lengths 0, 1, and >1 of input time array
    for periods in range(3):
        times = pd.date_range(start=start, freq='M', periods=periods)
        times = pd.to_datetime(times.values)  # Workaround for pandas bug
        actual = apply_time_offset(xr.DataArray(times), years=years,
                                   months=months, days=days, hours=hours)
        desired = (times + pd.DateOffset(
            years=years, months=months, days=days, hours=hours
        ))
        assert actual.identical(desired) 
Example 31
Project: aospy   Author: spencerahill   File: test_utils_times.py    Apache License 2.0 5 votes vote down vote up
def test_monthly_mean_ts_na():
    time = pd.to_datetime(['2000-06-01', '2001-06-01'])
    arr = xr.DataArray(np.random.random(time.shape), dims=[TIME_STR],
                       coords={TIME_STR: time})
    arr = arr.resample(**{TIME_STR: '1M'}).mean(TIME_STR)
    actual = monthly_mean_ts(arr)
    desired = arr.dropna(TIME_STR)
    assert desired.identical(actual) 
Example 32
Project: OpenAPS   Author: medicinexlab   File: bgdata.py    MIT License 5 votes vote down vote up
def _get_date(start_str, end_str):
    start_date = pd.to_datetime(start_str, format='%Y-%m-%d')
    end_date = pd.to_datetime(end_str, format='%Y-%m-%d')

    return start_date, end_date


#Function to find the start and stop indices for both the train and test dates given the dataframe, the start and end datetimes, and the boolean make_col_bool
#The start_index is a higher number than the end_index because the higher indices are the earlier dates while the lower indices are the later dates 
Example 33
Project: GeoLibs-Dator   Author: GeographicaGS   File: utils.py    MIT License 5 votes vote down vote up
def _set_datetime(data):
    dt_data = pd.to_datetime(data)
    return dt_data.dt.tz_convert(None) 
Example 34
Project: gamry-parser   Author: bcliang   File: test_chronoamperometry.py    MIT License 5 votes vote down vote up
def test_use_datetime(self):
        gp = parser.ChronoAmperometry(filename='tests/chronoa_data.dta', to_timestamp=False)
        gp.load()
        curve = gp.get_curve_data()
        # 'T' should return elapsed time in seconds
        self.assertEqual(curve['T'][0], 0)
        self.assertEqual(curve['T'].iloc[-1], 270)

        gp = parser.ChronoAmperometry(filename='tests/chronoa_data.dta', to_timestamp=True)
        gp.load()
        curve = gp.get_curve_data()
        # 'T' should return datetime objects
        self.assertEqual(curve['T'][0], pd.to_datetime('3/10/2019 12:00:00'))
        self.assertEqual(curve['T'].iloc[-1], pd.to_datetime('3/10/2019 12:04:30')) 
Example 35
Project: oslodatascience-rl   Author: Froskekongen   File: common.py    MIT License 5 votes vote down vote up
def readLogPong(filename, **kwargs):
    '''
    Get pong log file (LogPong) as a dataframe.
    filename: file name of log file.
    **kwargs: arguments passed to pd.read_csv.
    '''
    df = pd.read_csv(filename, sep=';', names=('time', 'episode', 'rewardSum'), **kwargs)
    df.time = pd.to_datetime(df.time)
    return df 
Example 36
Project: oslodatascience-rl   Author: Froskekongen   File: plotLog.py    MIT License 5 votes vote down vote up
def parse(self, line):
        line = self.removeNewLineCharacter(line).split(';')
        line[0] = pd.to_datetime(line[0])
        line[1] = int(line[1])
        line[2] = float(line[2])
        return line 
Example 37
Project: pymapd-examples   Author: omnisci   File: parsing_utils.py    Apache License 2.0 5 votes vote down vote up
def format_date_cols(df, col_list, tf = None, un = None):
    if col_list != {}:
            for col in col_list:
                df[col] = pd.to_datetime(df[col], format=tf, unit=un) 
Example 38
Project: purple_air_api   Author: ReagentX   File: sensor.py    GNU General Public License v3.0 5 votes vote down vote up
def get_historical(self, weeks_to_get: int) -> pd.DataFrame:
        '''Get data from the ThingSpeak API one week at a time up to weeks_to_get weeks in the past'''
        from_week = datetime.now()
        to_week = from_week - timedelta(weeks=1)
        url = f'https://thingspeak.com/channels/{self.tp_a}/feed.csv?api_key={self.tp_a_key}&offset=0&average=&round=2&start={to_week.strftime("%Y-%m-%d")}%2000:00:00&end={from_week.strftime("%Y-%m-%d")}%2000:00:00'
        df = pd.read_csv(url)
        if weeks_to_get > 1:
            for i in range(weeks_to_get):
                from_week = to_week  # DateTimes are immutable so this reference is not a problem
                to_week = to_week - timedelta(weeks=1)
                url = f'https://thingspeak.com/channels/{self.tp_a}/feed.csv?api_key={self.tp_a_key}&offset=0&average=&round=2&start={to_week.strftime("%Y-%m-%d")}%2000:00:00&end={from_week.strftime("%Y-%m-%d")}%2000:00:00'
                df = pd.concat([df, pd.read_csv(url)])

        # Handle formatting the DataFrame
        df.rename(columns={'field1': 'PM1 CF=ATM ug/m3',
                           'field2': 'PM25 CF=ATM ug/m3',
                           'field3': 'PM10 CF=ATM ug/m3',
                           'field4': 'Free HEAP memory',
                           'field5': 'ADC0 Voltage',
                           'field6': 'Sensor Firmware',
                           'field7': 'Unused',
                           'field8': 'PM25 CF=1 ug/m3'
                           }, inplace=True)
        df['created_at'] = pd.to_datetime(df['created_at'], format='%Y-%m-%d %H:%M:%S %Z')
        df.index = df.pop('entry_id')
        return df 
Example 39
Project: forex_backtesting   Author: Robinbeatrix   File: backup_bt.py    MIT License 5 votes vote down vote up
def df_converter(csv):
    df = pd.read_csv(csv)
    df['date'] = pd.to_datetime([x[:-21] for x in df['date'].squeeze().tolist()], dayfirst=True)
    df.drop_duplicates(subset='volume', keep=False, inplace=True)
    df.set_index('date', inplace=True)
    return df 
Example 40
Project: didi_competition   Author: Heipiao   File: main_load_data_DiDi.py    MIT License 5 votes vote down vote up
def order_sheet_pre():
    
    print("\n------ load order data ----------")
    order_sheet_path = os.path.join(LOAD_DATA_DIR, CONCRETE_DIR, ORDER_SHEET_DIR)
    print("load data from: " + order_sheet_path)
    save_path = os.path.join(SAVE_DATA_DIR, CONCRETE_DIR, ORDER_SHEET_DIR)
    print("save data to: " + save_path)

    for file in os.listdir(order_sheet_path):
        if "order_data_" in file:
            data = pd.read_csv(os.path.join(order_sheet_path,file),header=-1)
            
    #processing data and slice time
            data.columns = ['raw']
            data['order_id'] = data['raw'].map(lambda x: x.split("\t")[0])
            data['driver_id'] = data['raw'].map(lambda x: x.split("\t")[1])
            data['passenger_id'] = data['raw'].map(lambda x: x.split("\t")[2])
            data['start_district_hash'] = data['raw'].map(lambda x: x.split("\t")[3])
            data['dest_district_hash'] = data['raw'].map(lambda x: x.split("\t")[4])
            data['Price'] = data['raw'].map(lambda x: x.split("\t")[5])
            data['Time'] = data['raw'].map(lambda x: x.split("\t")[6])

            t = pd.to_datetime(data['Time'])
            data['Time']=t.map(lambda x:deal_the_day(x))

            data["week"] = data["Time"].map(lambda x: pd.to_datetime(x[:10]).weekday()+1)
            data["date"] = data["Time"].map(lambda x: x[:10])
            data["time"] = data["Time"].map(lambda x: x[11:])
            del data['raw']#del useless column
            data['Time']

    #save as the specific dir
            save_df_to_file(data, save_path, file) 
Example 41
Project: didi_competition   Author: Heipiao   File: main_load_data_DiDi.py    MIT License 5 votes vote down vote up
def traffic_sheet_pre():
#set filename and input data
    print("\n------ load traffic data ----------")
    traffic_sheet_path = os.path.join(LOAD_DATA_DIR, CONCRETE_DIR, TRAFFIC_SHEET_DIR)
    print("load data from: " + traffic_sheet_path)
    save_path =os.path.join(SAVE_DATA_DIR, CONCRETE_DIR, TRAFFIC_SHEET_DIR)
    print("save data to: " + save_path)

    for file in os.listdir(traffic_sheet_path):
        if "traffic_data_" in file:
            data = pd.read_csv(os.path.join(traffic_sheet_path,file),header=-1)
            
    #processing data and slice time 
            data.columns = ['raw']
            data['district_hash'] = data['raw'].map(lambda x: x.split("\t")[0])
            data['tj_level1_count'] = data['raw'].map(lambda x: x.split("\t")[1][2:])
            data['tj_level2_count'] = data['raw'].map(lambda x: x.split("\t")[2][2:])
            data['tj_level3_count'] = data['raw'].map(lambda x: x.split("\t")[3][2:])
            data['tj_level4_count'] = data['raw'].map(lambda x: x.split("\t")[4][2:])
            data['Time'] = data['raw'].map(lambda x: x.split("\t")[5])
            t = pd.to_datetime(data['Time'])
            data['Time']=t.map(lambda x:deal_the_day(x))

            data["week"] = data["Time"].map(lambda x: pd.to_datetime(x[:10]).weekday()+1)
            data["date"] = data["Time"].map(lambda x: x[:10])
            data["time"] = data["Time"].map(lambda x: x[11:])
            del data['raw']#del useless column
            del data['Time']

    #save as the specific dir
            
            save_df_to_file(data, save_path, file) 
Example 42
Project: didi_competition   Author: Heipiao   File: main_load_data_DiDi.py    MIT License 5 votes vote down vote up
def weather_sheet_pre():
#set filename and input data
    print("\n------ load weather data ----------")
    weather_sheet_path = os.path.join(LOAD_DATA_DIR, CONCRETE_DIR, WEATHER_SHEET_DIR)
    print("load data from: " + weather_sheet_path)
    save_path =os.path.join(SAVE_DATA_DIR, CONCRETE_DIR, WEATHER_SHEET_DIR)
    print("save data to: " + save_path)

    for file in os.listdir(weather_sheet_path):
        if "weather_data_" in file:
            data = pd.read_csv(os.path.join(weather_sheet_path,file),header=-1)
            
    #processing data and slice time 
            data.columns = ['raw']
            data['Time'] = data['raw'].map(lambda x: x.split("\t")[0])
            t = pd.to_datetime(data['Time'])
            data['Time']=t.map(lambda x:deal_the_day(x))
            data['Weather'] = data['raw'].map(lambda x: x.split("\t")[1])
            data['temperature'] = data['raw'].map(lambda x: x.split("\t")[2])
            data['PM2.5'] = data['raw'].map(lambda x: x.split("\t")[3])

            data["week"] = data["Time"].map(lambda x: pd.to_datetime(x[:10]).weekday()+1)
            data["date"] = data["Time"].map(lambda x: x[:10])
            data["time"] = data["Time"].map(lambda x: x[11:])
            del data['raw']#del useless column
            del data['Time']

    #save as the specific dir
            save_df_to_file(data, save_path, file) 
Example 43
Project: didi_competition   Author: Heipiao   File: load_data_DiDi.py    MIT License 5 votes vote down vote up
def order_sheet_pre():
#set filename and input data
    print("load data from:")
    for x in range(START,END,SEP):
        syspath = "../../season_1/"+IS_TRAINING+"/order_data/"
        if(x<10):
            name = "order_data_2016-01-0"+str(x)+_TEST
        else:
            name = "order_data_2016-01-"+str(x)+_TEST
        print(name)
        data = pd.read_csv(os.path.join(syspath,name),header=-1)
        
#processing data and slice time 
        print("process data...")
        data.columns = ['raw']
        data['order_id'] = data['raw'].map(lambda x: x.split("\t")[0])
        data['driver_id'] = data['raw'].map(lambda x: x.split("\t")[1])
        data['passenger_id'] = data['raw'].map(lambda x: x.split("\t")[2])
        data['start_district_hash'] = data['raw'].map(lambda x: x.split("\t")[3])
        data['dest_district_hash'] = data['raw'].map(lambda x: x.split("\t")[4])
        data['Price'] = data['raw'].map(lambda x: x.split("\t")[5])
        data['Time'] = data['raw'].map(lambda x: x.split("\t")[6])
        t = pd.to_datetime(data['Time'])
        data['Time']=t.map(lambda x:str(x.year)+'-'+str(x.month)+'-'+str(x.day)+'-'+str(x.hour*6+int(x.minute/10)+1))
        del data['raw']#del useless column

#save as the specific dir
        save_path ="../../season_1_sad/"+IS_TRAINING+"/order_data/"
        if os.path.isdir(save_path):
            print("save data at"+save_path+name)
            data.to_csv(os.path.join(save_path,name))
        else:
            os.makedirs(save_path)
            print("save data at"+save_path+name)
            data.to_csv(os.path.join(save_path,name)) 
Example 44
Project: didi_competition   Author: Heipiao   File: load_data_DiDi.py    MIT License 5 votes vote down vote up
def traffic_sheet_pre():
#set filename and input data
    print("load data from:")
    for x in range(START,END,SEP):
        syspath = "../../season_1/"+IS_TRAINING+"/traffic_data/"
        if(x<10):
            name = "traffic_data_2016-01-0"+str(x)+_TEST
        else:
            name = "traffic_data_2016-01-"+str(x)+_TEST
        print(name)
        data = pd.read_csv(os.path.join(syspath,name),header=-1)
        
#processing data and slice time 
        print("process data...")
        data.columns = ['raw']
        data['district_hash'] = data['raw'].map(lambda x: x.split("\t")[0])
        data['tj_level1_count'] = data['raw'].map(lambda x: x.split("\t")[1][2:])
        data['tj_level2_count'] = data['raw'].map(lambda x: x.split("\t")[2][2:])
        data['tj_level3_count'] = data['raw'].map(lambda x: x.split("\t")[3][2:])
        data['tj_level4_count'] = data['raw'].map(lambda x: x.split("\t")[4][2:])
        data['tj_time'] = data['raw'].map(lambda x: x.split("\t")[5])
        t = pd.to_datetime(data['tj_time'])
        data['tj_time']=t.map(lambda x:str(x.year)+'-'+str(x.month)+'-'+str(x.day)+'-'+str(x.hour*6+int(x.minute/10)+1))
        del data['raw']#del useless column

#save as the specific dir
        save_path ="../../season_1_sad/"+IS_TRAINING+"/traffic_data/"
        if os.path.isdir(save_path):
            print("save data at"+save_path+name)
            data.to_csv(os.path.join(save_path,name))
        else:
            os.makedirs(save_path)
            print("save data at"+save_path+name)
            data.to_csv(os.path.join(save_path,name)) 
Example 45
Project: HAPI   Author: MAfarrag   File: trial2.py    MIT License 5 votes vote down vote up
def update():
    current = df[(df['dat'] >=  pd.to_datetime(slider.value[0])) & (df['dat'] <=  pd.to_datetime(slider.value[1]))]
    source.data = {
        'opens'             : current.open,
        'dates'           : current.date,
      } 
Example 46
Project: fugle-realtime-py   Author: fortuna-intelligence   File: intraday.py    MIT License 5 votes vote down vote up
def chart(
    apiToken="demo",
    apiVersion="v0",
    host="api.fugle.tw",
    output="dataframe",
    symbolId="2884",
):
    outputs = ["dataframe", "raw"]
    if output not in outputs:
        raise ValueError('output must be one of ["dataframe", "raw"]')
    url = "https://{}/realtime/{}/intraday/chart".format(host, apiVersion)
    params = dict(apiToken=apiToken, symbolId=symbolId)
    response = get(url=url, params=params)
    json = response.json()
    if response.status_code != 200:
        if output == "dataframe":
            return json_normalize(json)
        elif output == "raw":
            return json
    chart = json["data"]["chart"]
    if output == "dataframe":
        chart = [dict(at=at, **rest) for at, rest in chart.items()]
        df = json_normalize(chart)
        if "at" in df.columns:
            df["at"] = to_datetime(df["at"])
            df = df.sort_values("at")
            df = df.reset_index(drop=True)
        return df
    elif output == "raw":
        return chart 
Example 47
Project: fugle-realtime-py   Author: fortuna-intelligence   File: intraday.py    MIT License 5 votes vote down vote up
def trades(
    apiToken="demo",
    apiVersion="v0",
    host="api.fugle.tw",
    output="dataframe",
    symbolId="2884",
):
    outputs = ["dataframe", "raw"]
    if output not in outputs:
        raise ValueError('output must be one of ["dataframe", "raw"]')
    url = "https://{}/realtime/{}/intraday/trades".format(host, apiVersion)
    params = dict(apiToken=apiToken, symbolId=symbolId)
    response = get(url=url, params=params)
    json = response.json()
    if response.status_code != 200:
        if output == "dataframe":
            return json_normalize(json)
        elif output == "raw":
            return json
    trades = json["data"]["trades"]
    if output == "dataframe":
        df = json_normalize(trades)
        if "at" in df.columns:
            df["at"] = to_datetime(df["at"])
            df = df.sort_values("at")
            df = df.reset_index(drop=True)
        return df
    elif output == "raw":
        return trades 
Example 48
Project: jupyterlab_code_formatter   Author: ryantam626   File: some_python.py    MIT License 5 votes vote down vote up
def read(fp):
    df = (pd.read_csv(fp)
            .rename(columns=str.lower)            .drop('unnamed: 36', axis=1)            .pipe(extract_city_name)            .pipe(time_to_datetime, ['dep_time', 'arr_time', 'crs_arr_time', 'crs_dep_time'])
            .assign(fl_date=lambda x: pd.to_datetime(x['fl_date']),
                    dest=lambda x: pd.Categorical(x['dest']),
                    origin=lambda x: pd.Categorical(x['origin']),                    tail_num=lambda x: pd.Categorical(x['tail_num']),                    unique_carrier=lambda x: pd.Categorical(x['unique_carrier']),
                    cancellation_code=lambda x: pd.Categorical(x['cancellation_code'])))
    return df 
Example 49
Project: xalpha   Author: refraction-ray   File: info.py    MIT License 5 votes vote down vote up
def update(self):
        lastdate = self.price.iloc[-1].date
        lastdatestr = lastdate.strftime("%Y%m%d")
        weight = self.price.iloc[1].totvalue
        self._updateurl = (
            "http://quotes.money.163.com/service/chddata.html?code="
            + self.code
            + "&start="
            + lastdatestr
            + "&end="
            + yesterday()
            + "&fields=TCLOSE"
        )
        df = pd.read_csv(self._updateurl, encoding="gb2312")
        self.name = df.iloc[0].loc["名称"]
        if len(df) > 1:
            df = df.rename(columns={"收盘价": "totvalue"})
            df["date"] = pd.to_datetime(df.日期)
            df = df.drop(["股票代码", "名称", "日期"], axis=1)
            df["netvalue"] = df.totvalue / weight
            df["comment"] = [0 for _ in range(len(df))]
            df = df.iloc[::-1].iloc[1:]
            df = df[df["date"].isin(opendate)]
            df = df.reset_index(drop=True)
            df = df[df["date"] <= yesterdayobj()]
            self.price = self.price.append(df, ignore_index=True, sort=True)
            return df 
Example 50
Project: xalpha   Author: refraction-ray   File: record.py    MIT License 5 votes vote down vote up
def __init__(self, path="input.csv", format="matrix", **readkwds):
        df = pd.read_csv(path, **readkwds)
        if format == "matrix":
            df.date = [
                # pd.Timestamp.strptime(str(int(df.iloc[i].date)), "%Y%m%d")
                # higher version of pandas timestamp doesn't support strptime anymore? why? what is the gain here?
                pd.to_datetime(str(int(df.iloc[i].date)), format="%Y%m%d")
                for i in range(len(df))
            ]
            df.fillna(0, inplace=True)
            self.status = df
        elif format == "list":
            fund = df.fund.unique()
            fund_s = ["{:06d}".format(i) for i in fund]
            date_s = df.date.unique()
            dfnew = pd.DataFrame(
                columns=["date"] + fund_s, index=date_s, dtype="float64"
            )
            dfnew.fillna(0, inplace=True)
            # dfnew["date"] = [pd.Timestamp.strptime(i, "%Y/%m/%d") for i in date_s]
            dfnew["date"] = [pd.to_datetime(i, format="%Y/%m/%d") for i in date_s]
            for i in range(len(df)):
                dfnew.at[df.iloc[i].date, "{:06d}".format(df.iloc[i].fund)] += df.iloc[
                    i
                ].trade
            dfnew = dfnew.sort_values(by=["date"])
            self.status = dfnew