Python pandas.date_range() Examples

The following are code examples for showing how to use pandas.date_range(). 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: backtrader-cn   Author: pandalibin   File: test_datas_utils.py    GNU General Public License v3.0 6 votes vote down vote up
def _test_strip_unused_cols(self):
        data = pd.DataFrame({
            'name': ['tom', 'jack'],
            'age': [24, 56],
            'gender': ['male', 'male'],
            'address': ['cn', 'us']
        })
        data.index = pd.date_range(start='2017-01-01', periods=2)

        origin_cols = ['name', 'age', 'gender', 'address']
        unused_cols = ['address', 'gender']
        new_cols = ['name', 'age']

        self.assertEqual(list(data.columns).sort(), origin_cols.sort())

        bdu.Utils.strip_unused_cols(data, *unused_cols)

        self.assertEqual(list(data.columns).sort(), new_cols.sort()) 
Example 2
Project: aospy   Author: spencerahill   File: times.py    Apache License 2.0 6 votes vote down vote up
def monthly_mean_at_each_ind(monthly_means, sub_monthly_timeseries):
    """Copy monthly mean over each time index in that month.

    Parameters
    ----------
    monthly_means : xarray.DataArray
        array of monthly means
    sub_monthly_timeseries : xarray.DataArray
        array of a timeseries at sub-monthly time resolution

    Returns
    -------
    xarray.DataArray with eath monthly mean value from `monthly_means` repeated
    at each time within that month from `sub_monthly_timeseries`

    See Also
    --------
    monthly_mean_ts : Create timeseries of monthly mean values
    """
    time = monthly_means[TIME_STR]
    start = time.indexes[TIME_STR][0].replace(day=1, hour=0)
    end = time.indexes[TIME_STR][-1]
    new_indices = pd.date_range(start=start, end=end, freq='MS')
    arr_new = monthly_means.reindex(time=new_indices, method='backfill')
    return arr_new.reindex_like(sub_monthly_timeseries, method='pad') 
Example 3
Project: HAPI   Author: MAfarrag   File: main.py    MIT License 6 votes vote down vote up
def changetimestep(attr,old,new):
    s=dt.datetime(int(startdate.value[:4]),int(startdate.value[5:7]),int(startdate.value[8:10]),
                  int(startdate.value[11:13]),int(startdate.value[14:16]),int(startdate.value[17:]))
    e=dt.datetime(int(endtdate.value[:4]),int(endtdate.value[5:7]),int(endtdate.value[8:10]),
                  int(endtdate.value[11:13]),int(endtdate.value[14:16]),int(endtdate.value[17:]))
    
    if Time_step.value== "Hourly":
        freq="1H"
    elif Time_step.value== "Daily":
        freq="1D"
    elif Time_step.value== "weekly":
        freq="1W"
    dates=pd.date_range(s,e,freq=freq).tolist()
    dates=[str(i) for i in dates]
    # update the date_ds to the new time series dates
    date_ds.data['date']=dates 
Example 4
Project: xalpha   Author: refraction-ray   File: info.py    MIT License 6 votes vote down vote up
def _basic_init(self):
        self.name = "货币基金"
        self.rate = 0
        datel = list(
            pd.date_range(dt.datetime.strftime(self.start, "%Y-%m-%d"), yesterdaydash())
        )
        valuel = []
        for i, date in enumerate(datel):
            valuel.append((1 + self.interest) ** i)
        dfdict = {
            "date": datel,
            "netvalue": valuel,
            "totvalue": valuel,
            "comment": [0 for _ in datel],
        }
        df = pd.DataFrame(data=dfdict)
        self.price = df[df["date"].isin(opendate)] 
Example 5
Project: NeuroKit   Author: neuropsychology   File: signal_resample.py    MIT License 6 votes vote down vote up
def _resample_pandas(signal, desired_length):
    # Convert to Time Series
    index = pd.date_range('20131212', freq='L', periods=len(signal))
    resampled_signal = pd.Series(signal, index=index)

    # Create resampling factor
    resampling_factor = str(1/(desired_length / len(signal))) + "L"

    # Resample
    resampled_signal = resampled_signal.resample(resampling_factor).bfill().values

    # Sanitize
    resampled_signal = _resample_sanitize(resampled_signal, desired_length)

    return(resampled_signal)


# =============================================================================
# Internals
# ============================================================================= 
Example 6
Project: Aegis   Author: jlillywh   File: clock.py    GNU General Public License v3.0 6 votes vote down vote up
def set_start_date(self, new_date):
        """Change the start date before running a new simulation
            It is assumed that you want the end date to also change when
            you change the start date because the duration would be held
            constant. Therefore, the end_date is also adjusted here. The
            current date is also reset to the start date.

            The "running" state is also set to True.

            Parameters
            ----------
            new_date : str
                string formats allowed: 'mm/dd/yyyy'; 'mm-dd-yyyy'

            Returns
            -------
            """
        self.start_date = pd.Timestamp(new_date, freq=self.time_step)
        self.current_date = self.start_date
        self.end_date = self.current_date + self.duration
        self.running = True
        self.range = pd.date_range(start=self.start_date, end=self.end_date) 
Example 7
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 8
Project: Aegis   Author: jlillywh   File: sliding_ts_chart.py    GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, values=random.sample(range(100), 10), date='1/1/2019', size=10):
        """
            Parameters
            ----------
            values : list of numbers
            date : str
                The date of the value on far right of array
            size : int
                The size of the array
        """
        TimeHistoryChart.__init__(self)
        self.last_date = pd.Timestamp(date)
        first_date = self.last_date - pd.Timedelta(str(size) + ' days')
        self.dates = pd.date_range(first_date, periods=size)
        self.size = size
        self.queue = deque(values)
        self.data = pd.DataFrame(np.random.randn(6), index=self.dates, columns='A') 
Example 9
Project: pysat   Author: pysat   File: test_instrument.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_iterate_over_bounds_set_by_date_season_extra_time(self):
        start = [pysat.datetime(2009, 1, 1, 1, 10),
                 pysat.datetime(2009, 2, 1, 1, 10)]
        stop = [pysat.datetime(2009, 1, 15, 1, 10),
                pysat.datetime(2009, 2, 15, 1, 10)]
        self.testInst.bounds = (start, stop)
        # filter
        start = self.testInst._filter_datetime_input(start)
        stop = self.testInst._filter_datetime_input(stop)
        # iterate
        dates = []
        for inst in self.testInst:
            dates.append(inst.date)
        out = pds.date_range(start[0], stop[0]).tolist()
        out.extend(pds.date_range(start[1], stop[1]).tolist())
        assert np.all(dates == out) 
Example 10
Project: pysat   Author: pysat   File: test_instrument.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_iterate_over_bounds_set_by_fname_via_next(self):
        start = '2009-01-01.nofile'
        stop = '2009-01-15.nofile'
        start_d = pysat.datetime(2009, 1, 1)
        stop_d = pysat.datetime(2009, 1, 15)
        self.testInst.bounds = (start, stop)
        dates = []
        loop_next = True
        while loop_next:
            try:
                self.testInst.next()
                dates.append(self.testInst.date)
            except StopIteration:
                loop_next = False
        out = pds.date_range(start_d, stop_d).tolist()
        assert np.all(dates == out) 
Example 11
Project: pysat   Author: pysat   File: test_instrument.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_iterate_over_bounds_set_by_fname_via_prev(self):
        start = '2009-01-01.nofile'
        stop = '2009-01-15.nofile'
        start_d = pysat.datetime(2009, 1, 1)
        stop_d = pysat.datetime(2009, 1, 15)
        self.testInst.bounds = (start, stop)
        dates = []
        loop = True
        while loop:
            try:
                self.testInst.prev()
                dates.append(self.testInst.date)
            except StopIteration:
                loop = False
        out = pds.date_range(start_d, stop_d).tolist()
        assert np.all(dates == out[::-1]) 
Example 12
Project: arctic   Author: man-group   File: test_toplevel.py    GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_should_return_data_when_date_range_spans_libraries(toplevel_tickstore, arctic):
    arctic.initialize_library('FEED_2010.LEVEL1', tickstore.TICK_STORE_TYPE)
    arctic.initialize_library('FEED_2011.LEVEL1', tickstore.TICK_STORE_TYPE)
    tickstore_2010 = arctic['FEED_2010.LEVEL1']
    tickstore_2011 = arctic['FEED_2011.LEVEL1']
    toplevel_tickstore.add(DateRange(start=dt(2010, 1, 1), end=dt(2010, 12, 31, 23, 59, 59, 999000)), 'FEED_2010.LEVEL1')
    toplevel_tickstore.add(DateRange(start=dt(2011, 1, 1), end=dt(2011, 12, 31, 23, 59, 59, 999000)), 'FEED_2011.LEVEL1')
    dates = pd.date_range('20100101', periods=6, tz=mktz('Europe/London'))
    df_10 = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD'))
    tickstore_2010.write('blah', df_10)
    dates = pd.date_range('20110101', periods=6, tz=mktz('Europe/London'))
    df_11 = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD'))
    tickstore_2011.write('blah', df_11)
    res = toplevel_tickstore.read('blah', DateRange(start=dt(2010, 1, 2), end=dt(2011, 1, 4)), list('ABCD'))
    expected_df = pd.concat([df_10[1:], df_11[:4]])
    assert_frame_equal(expected_df, res.tz_convert(mktz('Europe/London'))) 
Example 13
Project: arctic   Author: man-group   File: test_toplevel.py    GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_should_return_data_when_date_range_spans_libraries_even_if_one_returns_nothing(toplevel_tickstore, arctic):
    arctic.initialize_library('FEED_2010.LEVEL1', tickstore.TICK_STORE_TYPE)
    arctic.initialize_library('FEED_2011.LEVEL1', tickstore.TICK_STORE_TYPE)
    tickstore_2010 = arctic['FEED_2010.LEVEL1']
    tickstore_2011 = arctic['FEED_2011.LEVEL1']
    toplevel_tickstore.add(DateRange(start=dt(2010, 1, 1), end=dt(2010, 12, 31, 23, 59, 59, 999000)), 'FEED_2010.LEVEL1')
    toplevel_tickstore.add(DateRange(start=dt(2011, 1, 1), end=dt(2011, 12, 31, 23, 59, 59, 999000)), 'FEED_2011.LEVEL1')
    dates = pd.date_range('20100101', periods=6, tz=mktz('Europe/London'))
    df_10 = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD'))
    tickstore_2010.write('blah', df_10)
    dates = pd.date_range('20110201', periods=6, tz=mktz('Europe/London'))
    df_11 = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD'))
    tickstore_2011.write('blah', df_11)
    res = toplevel_tickstore.read('blah', DateRange(start=dt(2010, 1, 2), end=dt(2011, 1, 4)), list('ABCD'))
    expected_df = df_10[1:]
    assert_frame_equal(expected_df, res.tz_convert(mktz('Europe/London'))) 
Example 14
Project: arctic   Author: man-group   File: test_toplevel.py    GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_should_successfully_do_a_roundtrip_write_and_read_spanning_multiple_underlying_libraries(toplevel_tickstore, arctic):
    arctic.initialize_library('FEED_2010.LEVEL1', tickstore.TICK_STORE_TYPE)
    arctic.initialize_library('FEED_2011.LEVEL1', tickstore.TICK_STORE_TYPE)
    arctic.initialize_library('test_current.toplevel_tickstore', tickstore.TICK_STORE_TYPE)
    toplevel_tickstore.add(DateRange(start=dt(2010, 1, 1), end=dt(2010, 12, 31, 23, 59, 59, 999000)), 'FEED_2010.LEVEL1')
    toplevel_tickstore.add(DateRange(start=dt(2011, 1, 1), end=dt(2011, 12, 31, 23, 59, 59, 999000)), 'FEED_2011.LEVEL1')
    tickstore_current = arctic['test_current.toplevel_tickstore']
    dates = pd.date_range('20101201', periods=57, tz=mktz('Europe/London'))
    data = pd.DataFrame(np.random.randn(57, 4), index=dates, columns=list('ABCD'))
    toplevel_tickstore.write('blah', data)
    tickstore_current.write('blah', data)
    res = toplevel_tickstore.read('blah', DateRange(start=dt(2010, 12, 1), end=dt(2011, 2, 1)), columns=list('ABCD'))
    assert_frame_equal(data, res.tz_convert(mktz('Europe/London')))
    lib2010 = arctic['FEED_2010.LEVEL1']
    res = lib2010.read('blah', DateRange(start=dt(2010, 12, 1), end=dt(2011, 1, 1)), columns=list('ABCD'))
    assert_frame_equal(data[dt(2010, 12, 1): dt(2010, 12, 31)], res.tz_convert(mktz('Europe/London')))
    lib2011 = arctic['FEED_2011.LEVEL1']
    res = lib2011.read('blah', DateRange(start=dt(2011, 1, 1), end=dt(2011, 2, 1)), columns=list('ABCD'))
    assert_frame_equal(data[dt(2011, 1, 1): dt(2011, 2, 1)], res.tz_convert(mktz('Europe/London'))) 
Example 15
Project: arctic   Author: man-group   File: test_toplevel.py    GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_should_write_top_level_with_correct_timezone(arctic):
    # Write timezone aware data and read back in UTC
    utc = mktz('UTC')
    arctic.initialize_library('FEED_2010.LEVEL1', tickstore.TICK_STORE_TYPE)
    arctic.initialize_library('FEED_2011.LEVEL1', tickstore.TICK_STORE_TYPE)
    arctic.initialize_library('FEED.LEVEL1', toplevel.TICK_STORE_TYPE)
    toplevel_tickstore = arctic['FEED.LEVEL1']
    dates = pd.date_range('20101230220000', periods=10, tz=mktz('America/New_York'))  # 10pm New York time is 3am next day UTC 
    data = [{'index': dates[i], 'a': i} for i in range(len(dates))]
    expected = pd.DataFrame(np.arange(len(dates), dtype=np.float64), index=dates.tz_convert(utc), columns=list('a'))
    toplevel_tickstore.write('blah', data)
    res = toplevel_tickstore.read('blah', DateRange(start=dt(2010, 1, 1), end=dt(2011, 12, 31)), columns=list('a')).tz_convert(utc)
    assert_frame_equal(expected, res)
    lib2010 = arctic['FEED_2010.LEVEL1']
    # Check that only one point was written into 2010 being 3am on 31st
    assert len(lib2010.read('blah', DateRange(start=dt(2010, 12, 1), end=dt(2011, 1, 1)))) == 1 
Example 16
Project: arctic   Author: man-group   File: test_fixes.py    GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_rewrite(chunkstore_lib):
    """
    Issue 427
    incorrectly storing and updating metadata. dataframes without an index
    have no "index" field in their metadata, so updating existing
    metadata does not remove the index field.
    Also, metadata was incorrectly being stored. symbol, start, and end
    are the index for the collection, but metadata was being
    stored without an index (so it was defaulting to null,null,null)
    """
    date_range = pd.date_range(start=dt(2017, 5, 1, 1), periods=8, freq='6H')

    df = DataFrame(data={'something': [100, 200, 300, 400, 500, 600, 700, 800]},
                   index=DatetimeIndex(date_range, name='date'))

    chunkstore_lib.write('test', df, chunk_size='D')

    df2 = DataFrame(data={'something': [100, 200, 300, 400, 500, 600, 700, 800],
                          'date': date_range})

    chunkstore_lib.write('test', df2, chunk_size='D')
    ret = chunkstore_lib.read('test')
    assert_frame_equal(ret, df2) 
Example 17
Project: arctic   Author: man-group   File: test_fixes.py    GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_missing_cols(chunkstore_lib):
    index = DatetimeIndex(pd.date_range('2019-01-01', periods=3, freq='D'), name='date')
    index2 = DatetimeIndex(pd.date_range('2019-01-04', periods=3, freq='D'), name='date')
    expected_index = DatetimeIndex(pd.date_range('2019-01-01', periods=6, freq='D'), name='date')
    expected_df = DataFrame({'A': [1, 2, 3, 40, 50, 60], 'B': [5.0,6.0,7.0, np.nan, np.nan, np.nan]}, index=expected_index)

    df = pd.DataFrame({'A': [1, 2, 3], 'B': [5,6,7]}, index=index)
    chunkstore_lib.write('test', df, chunk_size='D')

    df = pd.DataFrame({'A': [40, 50, 60]}, index=index2)
    chunkstore_lib.append('test', df, chunk_size='D')


    assert_frame_equal(chunkstore_lib.read('test'), expected_df)
    df = chunkstore_lib.read('test', columns=['B'])
    assert_frame_equal(df, expected_df['B'].to_frame()) 
Example 18
Project: arctic   Author: man-group   File: test_chunkstore.py    GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_list_symbols(chunkstore_lib):
    df = DataFrame(data={'data': np.random.randint(0, 100, size=366)},
                   index=pd.date_range('2016-01-01', '2016-12-31'))
    df.index.name = 'date'

    chunkstore_lib.write('rabbit', df)
    chunkstore_lib.write('dragon', df)
    chunkstore_lib.write('snake', df)
    chunkstore_lib.write('wolf', df)
    chunkstore_lib.write('bear', df)

    assert('dragon' in chunkstore_lib.list_symbols())
    assert(set(['rabbit', 'dragon', 'bear']) == set(chunkstore_lib.list_symbols(partial_match='r')))

    assert(chunkstore_lib.has_symbol('dragon'))
    assert(chunkstore_lib.has_symbol('marmot') is False) 
Example 19
Project: arctic   Author: man-group   File: test_chunkstore.py    GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_unsorted_date_col(chunkstore_lib):
    df = pd.DataFrame({'date': [dt(2016, 9, 1), dt(2016, 8, 31)],
                       'vals': range(2)})
    df2 = pd.DataFrame({'date': [dt(2016, 9, 2), dt(2016, 9, 1)],
                        'vals': range(2)})

    chunkstore_lib.write('test_symbol', df)
    try:
        df = df.sort_values('date')
    except AttributeError:
        df = df.sort(columns='date')
    assert_frame_equal(df.reset_index(drop=True), chunkstore_lib.read('test_symbol'))
    chunkstore_lib.update('test_symbol', df2)
    assert_frame_equal(chunkstore_lib.read('test_symbol'),
                       pd.DataFrame({'date': pd.date_range('2016-8-31',
                                                           '2016-9-2'),
                                     'vals': [1, 1, 0]})) 
Example 20
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 21
Project: aospy   Author: spencerahill   File: test_utils_times.py    Apache License 2.0 5 votes vote down vote up
def test_monthly_mean_ts_single_month():
    time = pd.date_range('2000-01-01', freq='6H', periods=4 * 31)
    arr = xr.DataArray(np.random.random(time.shape), dims=[TIME_STR],
                       coords={TIME_STR: time})
    desired = arr.mean(TIME_STR)
    actual = monthly_mean_ts(arr)
    np.testing.assert_allclose(actual, desired) 
Example 22
Project: aospy   Author: spencerahill   File: test_utils_times.py    Apache License 2.0 5 votes vote down vote up
def test_monthly_mean_ts_submonthly():
    time = pd.date_range('2000-01-01', freq='1D', periods=365 * 3)
    arr = xr.DataArray(np.random.random(time.shape), dims=[TIME_STR],
                       coords={TIME_STR: time})
    desired = arr.resample(**{TIME_STR: '1M'}).mean(TIME_STR)
    actual = monthly_mean_ts(arr)
    assert desired.identical(actual) 
Example 23
Project: aospy   Author: spencerahill   File: test_utils_times.py    Apache License 2.0 5 votes vote down vote up
def test_monthly_mean_ts_monthly():
    time = pd.date_range('2000-01-01', freq='1M', periods=120)
    arr = xr.DataArray(np.random.random(time.shape), dims=[TIME_STR],
                       coords={TIME_STR: time})
    actual = monthly_mean_ts(arr)
    assert arr.identical(actual) 
Example 24
Project: aospy   Author: spencerahill   File: test_utils_times.py    Apache License 2.0 5 votes vote down vote up
def test_extract_months():
    time = xr.DataArray(pd.date_range(start='2001-02-18', end='2002-07-12',
                                      freq='1D'), dims=[TIME_STR])
    months = 'mam'  # March-April-May
    desired = xr.concat([
        xr.DataArray(pd.date_range(start='2001-03-01', end='2001-05-31',
                                   freq='1D'), dims=[TIME_STR]),
        xr.DataArray(pd.date_range(start='2002-03-01', end='2002-05-31',
                                   freq='1D'), dims=[TIME_STR])
    ], dim=TIME_STR)
    actual = extract_months(time, months)
    xr.testing.assert_identical(actual, desired) 
Example 25
Project: aospy   Author: spencerahill   File: test_utils_times.py    Apache License 2.0 5 votes vote down vote up
def test_extract_months_single_month():
    time = xr.DataArray(pd.date_range(start='1678-01-01', end='1678-01-31',
                                      freq='1M'), dims=[TIME_STR])
    months = 1
    desired = time
    actual = extract_months(time, months)
    xr.testing.assert_identical(actual, desired) 
Example 26
Project: aospy   Author: spencerahill   File: test_utils_times.py    Apache License 2.0 5 votes vote down vote up
def test_assert_matching_time_coord():
    rng = pd.date_range('2000-01-01', '2001-01-01', freq='M')
    arr1 = xr.DataArray(rng, coords=[rng], dims=[TIME_STR])
    arr2 = xr.DataArray(rng, coords=[rng], dims=[TIME_STR])
    assert_matching_time_coord(arr1, arr2)

    arr2 = arr2.sel(**{TIME_STR: slice('2000-03', '2000-05')})
    with pytest.raises(ValueError):
        assert_matching_time_coord(arr1, arr2) 
Example 27
Project: xrft   Author: xgcm   File: test_xrft.py    MIT License 5 votes vote down vote up
def test_dft_1d_time(self):
        """Test the discrete Fourier transform function on timeseries data."""
        time = pd.date_range('2000-01-01', '2001-01-01', closed='left')
        Nt = len(time)
        da = xr.DataArray(np.random.rand(Nt), coords=[time], dims=['time'])

        ft = xrft.dft(da)

        # check that frequencies are correct
        dt = (time[1] - time[0]).total_seconds()
        freq_time_expected = np.fft.fftshift(np.fft.fftfreq(Nt, dt))
        npt.assert_allclose(ft['freq_time'], freq_time_expected) 
Example 28
Project: LHMP   Author: hydrogo   File: wfdei_to_lumped_dataframe.py    GNU General Public License v3.0 5 votes vote down vote up
def data_reading_cutting(path, lat, lon):
    data = xr.open_dataset(path, decode_times=False)
    data['time'] = pd.date_range('1979-01-01', '2014-12-31', name='time')
    return data.sel_points(lat = lat, lon = lon)

# convert our xarray dataframe to simple pandas dataframe 
Example 29
Project: LHMP   Author: hydrogo   File: wfdei_to_lumped_dataframe.py    GNU General Public License v3.0 5 votes vote down vote up
def xdata_to_pdata(xdata, var_name):
    pdata = pd.DataFrame(index=pd.date_range('1979-01-01', '2014-12-31', name='Date'))
    for i in range(xdata.dims['points']):
        pdata[var_name+'_'+str(i)] = xdata[var_name][i].data
    return pdata

# function for potential evaporation (PET) by (Oudin et al., 2005) 
Example 30
Project: aurora   Author: carnby   File: timeseries.py    MIT License 5 votes vote down vote up
def build_timeseries(queryset, start, end, unit='minutes', multiples=1, attribute='datetime', plain=False,
                     isoformat=True, normalize=False):
    valid_freqs = {
        'minutes': 't',
        'days': 'd',
        'weeks': 'w',
        'months': 'm',
        'years': 'a',
        'hours': 'h'
    }

    maybe_naive_units = pandas.date_range(start, end, freq='{0}{1}'.format(multiples, valid_freqs[unit]))
    try:
        units = [pytz.timezone("UTC").localize(d) for d in maybe_naive_units]
    except ValueError:
        units = maybe_naive_units

    pairs = zip(units[0:-2], units[1:])

    ts = [{
        'count': daterange_queryset(queryset, p[0].to_datetime(), p[1].to_datetime(), attribute=attribute).count(),
        'datetime': (p[0]).isoformat() if isoformat else p[0]
    } for p in pairs]

    if normalize:
        max_date = max(ts, key=lambda x: x['count'])
        max_val = float(max_date['count'])
        for t in ts:
            t['count'] = t['count'] / max_val

    if plain:
        return [x['count'] for x in ts]

    return ts 
Example 31
Project: xalpha   Author: refraction-ray   File: multiple.py    MIT License 5 votes vote down vote up
def v_positions_history(self, end=yesterdaydash(), **vkwds):
        """
        river chart visulization of positions ratio history
        use text size to avoid legend overlap in some sense, eg. legend_text_size=8
        """
        start = self.totcftable.iloc[0].date
        times = pd.date_range(start, end)
        tdata = []
        for date in times:
            sdata = sorted(
                [
                    (
                        date,
                        fob.briefdailyreport(date).get("currentvalue", 0),
                        fob.aim.name,
                    )
                    for fob in self.fundtradeobj
                ],
                key=lambda x: x[1],
                reverse=True,
            )
            tdata.extend(sdata)

        tr = ThemeRiver()
        tr.add(
            series_name=[foj.aim.name for foj in self.fundtradeobj],
            data=tdata,
            label_opts=opts.LabelOpts(is_show=False),
            singleaxis_opts=opts.SingleAxisOpts(type_="time", pos_bottom="10%"),
        )

        return tr.render_notebook() 
Example 32
Project: xalpha   Author: refraction-ray   File: policy.py    MIT License 5 votes vote down vote up
def __init__(self, infoobj, start, end=yesterdaydash(), totmoney=100000):
        self.aim = infoobj
        self.totmoney = totmoney
        self.price = infoobj.price[
            (infoobj.price["date"] >= start) & (infoobj.price["date"] <= end)
        ]
        if len(self.price) == 0:
            self.start = convert_date(start)
            self.end = convert_date(end)
            self.status = pd.DataFrame(data={"date": [], self.aim.code: []})
        else:
            self.start = self.price.iloc[0].date
            self.end = self.price.iloc[-1].date
            datel = []
            actionl = []
            times = pd.date_range(self.start, self.end)
            for date in times:
                action = self.status_gen(date)
                if action > 0:
                    datel.append(date)
                    actionl.append(action)
                elif action < 0:
                    datel.append(date)
                    actionl.append(action * 0.005)
            df = pd.DataFrame(data={"date": datel, self.aim.code: actionl})
            self.status = df 
Example 33
Project: xalpha   Author: refraction-ray   File: indicator.py    MIT License 5 votes vote down vote up
def _pricegenerate(self, name):
        """
        generate price table for mulfix class, the cinfo class has this attr by default
        """
        if getattr(self, "price", None) is None:
            times = pd.date_range(self.totcftable.iloc[0].date, yesterdayobj())
            netvalue = []
            for date in times:
                netvalue.append(self.unitvalue(date))
            self.price = pd.DataFrame(data={"date": times, "netvalue": netvalue})
            self.price = self.price[self.price["date"].isin(opendate)]
            self.name = name 
Example 34
Project: xalpha   Author: refraction-ray   File: test_trade.py    MIT License 5 votes vote down vote up
def test_policy_scheduled():
    auto = xa.policy.scheduled(
        cm, 1000, pd.date_range("2015-07-01", "2018-07-01", freq="W-THU")
    )
    cm_t3 = xa.trade(cm, auto.status)
    cm_t3.v_tradevolume(freq="W")
    assert round(cm_t3.dailyreport("2018-08-03").iloc[0]["投资收益率"], 2) == -42.07
    auto2 = xa.policy.scheduled_tune(
        cm,
        1000,
        pd.date_range("2015-07-01", "2018-07-01", freq="M"),
        [(0.9, 2), (1.2, 1)],
    ) 
Example 35
Project: watttime-python-client   Author: WattTime   File: client.py    Apache License 2.0 5 votes vote down vote up
def get_impact_between(self, start_ts, end_ts, interval_minutes, ba,
                           market='RT5M', fill=True):
        """
        Get a pandas series of the marginal carbon impact
        for the given time range, interval length, and BA,
        using the RT5M market by default.
        By default, forward fills missing data; turn this off with fill=False.
        """
        # utcify
        try:  # aware
            utc_start = start_ts.astimezone(pytz.utc)
            utc_end = end_ts.astimezone(pytz.utc)
        except ValueError:  # naive
            try:
                utc_start = pytz.utc.localize(start_ts)
                utc_end = pytz.utc.localize(end_ts)
            except ValueError:
                raise ValueError('start_ts and end_ts must be both aware or both naive')

        # set up datetime index with correct interval
        dtidx = pd.date_range(utc_start, utc_end, freq='%dMin' % interval_minutes)

        # get cached value for every timestamp
        values = dtidx.map(lambda ts: self.get_impact_at(ts, ba, market))

        # set up series
        series = pd.Series(values, index=dtidx)

        # fill any remaining null values
        if fill:
            series = series.ffill()

        # return
        return series 
Example 36
Project: Aegis   Author: jlillywh   File: clock.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, start_date='1/1/2019', end_date='1/1/2020', time_step='1 days'):
        Aegis.__init__(self)
        self.description = "Clock to keep track of simulation time."
        ## Static time variables
        self.start_date = pd.Timestamp(start_date)
        self.end_date = pd.Timestamp(end_date)
        self.duration = self.end_date - self.start_date
        self.range = pd.date_range(start=self.start_date, end=self.end_date)
        self.time_step = pd.Timedelta(time_step)

        ## Dynamic time variables
        self.current_date = self.start_date
        #self.jday = self.current_date.timetuple().tm_yday
        self.remaining_time = self.duration
        self.running = True 
Example 37
Project: Aegis   Author: jlillywh   File: simulator.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, time_basis='elapsed time', duration=100, time_step='1 days'):
        Aegis.__init__(self)
        self.time_basis = time_basis
        self.c = Clock()
        self.ts = pd.Series(0, index=pd.date_range(self.c.start_date, periods=365, freq='D')) 
Example 38
Project: pysat   Author: pysat   File: pysat_testing2d.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def list_files(tag=None, sat_id=None, data_path=None, format_str=None):
    """Produce a fake list of files spanning a year"""

    index = pds.date_range(pysat.datetime(2008, 1, 1),
                           pysat.datetime(2010, 12, 31))
    names = [data_path + date.strftime('%Y-%m-%d') + '.nofile'
             for date in index]
    return pysat.Series(names, index=index) 
Example 39
Project: pysat   Author: pysat   File: pysat_testing_xarray.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def list_files(tag=None, sat_id=None, data_path=None, format_str=None):
    """Produce a fake list of files spanning a year"""

    index = pds.date_range(pysat.datetime(2008, 1, 1),
                           pysat.datetime(2010, 12, 31))
    names = [data_path+date.strftime('%Y-%m-%d')+'.nofile' for date in index]
    return pysat.Series(names, index=index) 
Example 40
Project: pysat   Author: pysat   File: pysat_testing2d_xarray.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def list_files(tag=None, sat_id=None, data_path=None, format_str=None):
    """Produce a fake list of files spanning a year"""

    index = pds.date_range(pysat.datetime(2008, 1, 1),
                           pysat.datetime(2010, 12, 31))
    names = [data_path + date.strftime('%Y-%m-%d') + '.nofile'
             for date in index]
    return pysat.Series(names, index=index) 
Example 41
Project: pysat   Author: pysat   File: pysat_testing.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def init(inst):
    """ Initialization function

    Shifts time index of files by 5-minutes if mangle_file_dates
    set to True at pysat.Instrument instantiation.

    Creates a file list for a given range if the file_date_range
    keyword is set at instantiation.

    Parameters
    ----------
    file_date_range : (pds.date_range)
        Optional keyword argument that specifies the range of dates for which
        test files will be created
    mangle_file_dates : bool
        If True, the loaded file list time index is shifted by 5-minutes.

    """
    inst.new_thing = True

    # work on file index if keyword present
    if 'file_date_range' in inst.kwargs:
        # set list files routine to desired date range
        # attach to the instrument object
        fdr = inst.kwargs['file_date_range']
        inst._list_rtn = functools.partial(list_files, file_date_range=fdr)
        inst.files.refresh()

    # mess with file dates if kwarg option present
    if 'mangle_file_dates' in inst.kwargs:
        if inst.kwargs['mangle_file_dates']:
            inst.files.files.index = \
                inst.files.files.index + pds.DateOffset(minutes=5) 
Example 42
Project: pysat   Author: pysat   File: pysat_testing.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def list_files(tag=None, sat_id=None, data_path=None, format_str=None,
               file_date_range=None):
    """Produce a fake list of files spanning a year

    Parameters
    ----------
    tag : (str)
        pysat instrument tag (default=None)
    sat_id : (str)
        pysat satellite ID tag (default=None)
    data_path : (str)
        pysat data path (default=None)
    format_str : (str)
        file format string (default=None)
    file_date_range : (pds.date_range)
        File date range (default=None)

    Returns
    -------
    Series of filenames indexed by file time

    """

    # Determine the appropriate date range for the fake files
    if file_date_range is None:
        start = _test_dates[''][''] - pds.DateOffset(years=1)
        stop = _test_dates[''][''] + pds.DateOffset(years=2) \
            - pds.DateOffset(days=1)
        file_date_range = pds.date_range(start, stop)

    index = file_date_range

    # Create the list of fake filenames
    names = [data_path + date.strftime('%Y-%m-%d') + '.nofile'
             for date in index]

    return pysat.Series(names, index=index) 
Example 43
Project: pysat   Author: pysat   File: test_instrument.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_set_bounds_with_frequency(self):
        start = pysat.datetime(2009, 1, 1)
        stop = pysat.datetime(2010, 1, 15)
        self.testInst.bounds = (start, stop, 'M')
        assert np.all(self.testInst._iter_list == pds.date_range(start, stop,
                                                                 freq='M').tolist()) 
Example 44
Project: pysat   Author: pysat   File: test_instrument.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_set_bounds_by_date(self):
        start = pysat.datetime(2009, 1, 1)
        stop = pysat.datetime(2009, 1, 15)
        self.testInst.bounds = (start, stop)
        assert np.all(self.testInst._iter_list ==
                      pds.date_range(start, stop).tolist()) 
Example 45
Project: pysat   Author: pysat   File: test_instrument.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_set_bounds_by_default(self):
        start = self.testInst.files.start_date
        stop = self.testInst.files.stop_date
        self.testInst.bounds = (None, None)
        assert np.all(self.testInst._iter_list ==
                      pds.date_range(start, stop).tolist())
        self.testInst.bounds = None
        assert np.all(self.testInst._iter_list ==
                      pds.date_range(start, stop).tolist())
        self.testInst.bounds = (start, None)
        assert np.all(self.testInst._iter_list ==
                      pds.date_range(start, stop).tolist())
        self.testInst.bounds = (None, stop)
        assert np.all(self.testInst._iter_list ==
                      pds.date_range(start, stop).tolist()) 
Example 46
Project: pysat   Author: pysat   File: test_instrument.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_set_bounds_by_date_extra_time(self):
        start = pysat.datetime(2009, 1, 1, 1, 10)
        stop = pysat.datetime(2009, 1, 15, 1, 10)
        self.testInst.bounds = (start, stop)
        start = self.testInst._filter_datetime_input(start)
        stop = self.testInst._filter_datetime_input(stop)
        assert np.all(self.testInst._iter_list ==
                      pds.date_range(start, stop).tolist()) 
Example 47
Project: pysat   Author: pysat   File: test_instrument.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_iterate_over_bounds_set_by_date(self):
        start = pysat.datetime(2009, 1, 1)
        stop = pysat.datetime(2009, 1, 15)
        self.testInst.bounds = (start, stop)
        dates = []
        for inst in self.testInst:
            dates.append(inst.date)
        out = pds.date_range(start, stop).tolist()
        assert np.all(dates == out) 
Example 48
Project: pysat   Author: pysat   File: test_instrument.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_iterate_over_default_bounds(self):
        start = self.testInst.files.start_date
        stop = self.testInst.files.stop_date
        self.testInst.bounds = (None, None)
        dates = []
        for inst in self.testInst:
            dates.append(inst.date)
        out = pds.date_range(start, stop).tolist()
        assert np.all(dates == out) 
Example 49
Project: pysat   Author: pysat   File: test_instrument.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_set_bounds_by_date_season(self):
        start = [pysat.datetime(2009, 1, 1), pysat.datetime(2009, 2, 1)]
        stop = [pysat.datetime(2009, 1, 15), pysat.datetime(2009, 2, 15)]
        self.testInst.bounds = (start, stop)
        out = pds.date_range(start[0], stop[0]).tolist()
        out.extend(pds.date_range(start[1], stop[1]).tolist())
        assert np.all(self.testInst._iter_list == out) 
Example 50
Project: pysat   Author: pysat   File: test_instrument.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_set_bounds_by_date_season_extra_time(self):
        start = [pysat.datetime(2009, 1, 1, 1, 10),
                 pysat.datetime(2009, 2, 1, 1, 10)]
        stop = [pysat.datetime(2009, 1, 15, 1, 10),
                pysat.datetime(2009, 2, 15, 1, 10)]
        self.testInst.bounds = (start, stop)
        start = self.testInst._filter_datetime_input(start)
        stop = self.testInst._filter_datetime_input(stop)
        out = pds.date_range(start[0], stop[0]).tolist()
        out.extend(pds.date_range(start[1], stop[1]).tolist())
        assert np.all(self.testInst._iter_list == out)