varwg package

Subpackages

Submodules

varwg.config_template module

Central configuration file for all things VG. This dummy-file contains necessary variables used as configurations and suggestions for their values in the form of comments

Adjust this file for your needs and rename it to “config.py”.

varwg.config_template.R_m2mm(times_, data, var_names, inverse=False)[source]
varwg.config_template.array_gen(scalar)[source]
varwg.config_template.max_qsw(doys, lon=9.18, lat=47.66, **kwds)[source]

Use meteo.meteox2y to sum up hourly values of solar radiation for the specified days - of - year.

varwg.config_template.var_names_greek(var_names)[source]

varwg.ctimes module

varwg.ctimes.datetime2doy(dt)

Extracts the day of year as a float from the given datetimes.

varwg.ctimes.doy_distance(doy0, doys)

varwg.ecdf module

class varwg.ecdf.ECDF(data, data_min=None, data_max=None)[source]

Bases: object

Methods

cdf

plot_cdf

ppf

cdf(x=None)[source]
plot_cdf(fig=None, ax=None, *args, **kwds)[source]
ppf(p=None)[source]

varwg.helpers module

Just some functions I find myself writing or searching for again and again…

class varwg.helpers.ADict(dict=None, /, **kwargs)[source]

Bases: UserDict

Methods

clear()

get(k[,d])

items()

keys()

pop(k[,d])

If key is not found, d is returned if given, otherwise KeyError is raised.

popitem()

as a 2-tuple; but raise KeyError if D is empty.

setdefault(k[,d])

update([E, ]**F)

If E present and has a .keys() method, does: for k in E.keys(): D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v

values()

copy

fromkeys

class varwg.helpers.LegendSubtitleHandler[source]

Bases: object

Methods

legend_artist

legend_artist(legend, orig_handle, fontsize, handlebox)[source]
varwg.helpers.asscalar(func)[source]

Return the result as a scalar if it has len == 1.

varwg.helpers.cache(*names, **name_values)[source]

Use as a decorator, to supply *names attributes that can be used as a cache. The attributes are set to their default/None during compile time. The wrapped function also has a ‘clear_cache’-method to delete those variables.

Parameters:
*namesstr
varwg.helpers.chdir(dirname)[source]

Temporarily change the working directory with a with-statement.

varwg.helpers.chi2_test(x, y, k=None, n_parameters=0)[source]

Chi-square test for inequality. H0: x and y were sampled from the same distribution.

Parameters:
kint

Number of classes (bins)

Returns:
p_valuefloat
varwg.helpers.clear_def_cache(function, cache_names=None, cache_name_values=None)[source]

I often use a simplified function cache in the form of ‘function.attribute = value’. This function helps cleaning it up, i.e. setting them to None.

Parameters:
functionobject with settable attributes
cache_namessequence of str or None, optional

if None, function should have an attribute called _cache_names with names of attributes that are cached.

varwg.helpers.csv2dict(filename, *args, **kwds)[source]

Returns a dictionary containing the columns of a csv-file. The keys are taken from the first row of the file.

varwg.helpers.csv2list(filename, startfrom=None, delimiter=None, column_ids=None, conversions=None, comment='#')[source]

Returns a list of each column of a csv-file.

varwg.helpers.fourier_approx(data, order=4, size=None, how='longest')[source]

Approximate data with a Fourier transform, using the order number of frequencies with the highest amplitudes.

Parameters:
data1-dim ndarray
orderint, optional

Number of frequencies to account for.

sizeint, optional

Desired length of the output. If None, it will be the same as data.

how“longest” or “strongest”, optional
varwg.helpers.gaps(data)[source]

Return indices referring to start and end points of gaps (marked by nans in the given array ‘data’

Parameters:
data1dim ndarray, dtype float or bool
>>> import numpy as np
>>> a = np.arange(20.)
>>> a[-1] = np.nan
>>> gaps(a)
array([[19, 19]])
>>> a = np.arange(20.)
>>> a[[0, 3, 4, 5, 12, 13, -1]] = np.nan
>>> gaps(a)
array([[ 0, 0],

[ 3, 5], [12, 13], [19, 19]])

>>> a = np.arange(20.)
>>> a[[0, 1, 3, 4, 5, 12, -2, -1]] = np.nan
>>> gaps(a)
array([[ 0, 1],

[ 3, 5], [12, 12], [18, 19]])

varwg.helpers.hist(values, n_bins, dist=None, pdf=None, kde=False, fig=None, ax=None, discrete=False, figsize=None, legend=True, *args, **kwds)[source]

Plots a histogram and therotical or empirical densities.

varwg.helpers.interp_nonfin(values, times=None, max_interp=None, pad_periodic=False, mask=None)[source]

Remove nans from values by linear interpolation.

Parameters:
values2d array
times1d array, optional
max_interpint

Maximum number of subsequent nans to interpolate over.

Examples

>>> import numpy as np
>>> a = np.array([0., np.nan, 1., np.nan, np.nan, 4.])
>>> interp_nan(a)
array([ 0. ,  0.5,  1. ,  2. ,  3. ,  4. ])
>>> interp_nan(a, max_interp=1)
array([ 0. ,  0.5,  1. ,  nan,  nan,  4. ])
>>> a = np.arange(6, dtype=float).reshape((2, 3))
>>> a[0, 1] = np.nan
>>> interp_nan(a)
array([[ 0.,  1.,  2.],
       [ 3.,  4.,  5.]])
varwg.helpers.kde_gauss(dataset, evaluation_points=None, kernel_width=None, maxopt=500, return_width=False, verbose=False)[source]
Parameters:
dataset(T,) ndarray

input data as array of length T

evaluation points(N,) ndarray, optional if return_width=True

N points as array (e.g. xx=np.linspace(-3,3,100)

kernel_widthfloat, optional

kernel_width (eg 0.3) if set, no MLM-routine to infer optimal kernel width

maxoptint, optional

size of sample to optimize kernel_width, affects runtime strongly. depends on memorysize. max 1000 with 2GB mem

return_widthboolean, optional

Return the optimized kernel width and nothing else.

verboseboolean, optional

Print information (also from the optimizer).

Examples

dataset = np.array(np.random.normal(size=1e3)) xx = np.linspace(-3,3, 1e3) plt.plot(xx,kde_gauss(dataset,xx))

varwg.helpers.kendalls_tau(x, y)[source]

Kendall’s Rank correlation coefficient. See Hartung p.599f

Examples

>>> A = [8, 6, 5, 3.5, 1, 2, 3.5, 7]
>>> B = [6, 7.5, 4, 1, 2, 3, 5, 7.5]
>>> kendalls_tau(A, B)
0.5714285714285714
varwg.helpers.key_tree(dict_, level=0)[source]
varwg.helpers.list_transpose(list_)[source]

Transposes a “2-dim” nested list.

Examples

>>> list_transpose([[1, 2, 3], [4, 5, 6]])
[[1, 4], [2, 5], [3, 6]]
varwg.helpers.periodic_pad(values)[source]
varwg.helpers.pickle_cache(filepath_template='%s.pkl', clear_cache=False, warn=True)[source]

Use this as a function decorator to cache the result of a function as a pickle-file. The filename is determined by the arguments in the function. If the filename turns out to be too long, a hash of it is used.

varwg.helpers.recursive_diff(name, obj1, obj2, *, ignore_types=None, plot=False, verbose=False, diff=None)[source]

Show differences between two objects.

For debugging purposes mostly. Recurses into sequences and instances of the current module.

Parameters:
namestr or None

Name of the current object. If None, will be determined by str(obj1).

obj1object

First object to compare.

obj2object

Second object to compare.

ignore_typessequence or None, optional

Ignore differences for objects of these types. callables are ignored by default.

plotbool, optional

Plot 1- and 2-D np.ndarrays if they are different.

verbosebool, optional

Be more talkative.

Examples

tba

varwg.helpers.rel_ranks(values, method='average')[source]

Returns ranks of values in the range [0,1].

varwg.helpers.round_to_float(values, precision)[source]

Round to nearest precision.

>>> round_to_float([8, 12], 5.)
array([ 10.,  10.])
varwg.helpers.scale_yticks(event)[source]

Automagically make room for yticklabels. Use it like this:

fig = gcf() fig.canvas.mpl_connect(‘draw_event’, scale_yticks)

Stolen from the matplotlib-howto. Slightly changed, so it is possible to separate the function from the calling code. http://matplotlib.sourceforge.net/faq/howto_faq.html #automatically-make-room-for-tick-labels

varwg.helpers.splom(data, variable_names=None, f_kwds=None, h_kwds=None, s_kwds=None, opacity=0.1, highlight_mask=None, ticklabels=True, figsize=None, hists=True, facecolor=(0, 0.5, 0.5), edgecolor=(1, 1, 1), f_opacity=None, e_opacity=None, highlight_color='red')[source]

Scatter-plot matrix with interactive capabilities.

Parameters:
data(K,T) ndarray

K variables, T timesteps

variable_namessequence of strings, optional

Used to label the subplots.

f_kwdsdictionary, optional

Keyword arguments that are passed to plt.subplots.

h_kwdsdictionary, optional

Keyword arguments that are passed to the histogramm calls.

s_kwdsdictionary, optional

Keyword arguments that are passed to the scatter calls.

opacityfloat, optional

Opacity used for edgecolor parameter of scatter call.

highlight_mask(T,) boolean ndarray, optional

Mask of timesteps that will be highlighted in the scatter plots.

ticklabelsboolean, optional

Set to False to supress displaying x- and yticklabels.

figsizeNone or tuple of width, height, optional

Size of the figure.

histsboolean, optional

Plot histograms on the main diagonal.

varwg.helpers.square_subplots(n_variables, *args, **kwds)[source]

Similar to plt.subplots, but shares x-axes column-wise and y-axes row - wise. Main diagonal subplots only share x-axes.

varwg.helpers.sumup(values, width=24, times_=None, drop_extra=True, mean=False, middle_time=True, sum_to_nan=False, acceptable_nans=6, max_interp=3)[source]

Sum up width number of values along the rows. If there are surplus entries, they are dropped as if they were hot (Snoop Dog et al).

Examples

>>> import numpy as np
>>> a = np.arange(10.).reshape((2, 5))
>>> a
array([[ 0.,  1.,  2.,  3.,  4.],
       [ 5.,  6.,  7.,  8.,  9.]])
>>> sumup(a, 2)
array([[  1.,   5.],
       [ 11.,  15.]])
>>> sumup(a, 2, drop_extra=False)
array([[  1.,   5.,   8.],
       [ 11.,  15.,  18.]])
>>> sumup(a.ravel(), 5)
array([ 10.,  35.])
>>> a[0, 0] = np.nan
>>> sumup(a, 2, mean=True)
array([[ 1. ,  2.5],
       [ 5.5,  7.5]])
varwg.helpers.val2ind(values, value)[source]

Return the index of the nearest neighbor of value in values.

varwg.helpers.yscale_subplots(fig=None, per_type=False, regrid=False)[source]

Sets a common y-scale to all subplots. If per_type is set to True, y-scales are distinguished by the type of the subplots.

varwg.smoothing module

1dim smoothing and moving moments (smoothing)

smooth

1-dimensional smoothing Simplified & modified http://www.scipy.org/Cookbook/SignalSmooth

variance

Moving variance.

std

Moving standard deviation.

skew

Moving skew.

median

Moving median.

min

Moving minimum.

max

Moving maximum.

percentile

Moving percentile.

autocorr

Moving autocorrelation.

corr

Moving correlation.

crosscorr

Moving crosscorrelation.

varwg.smoothing.autocorr(data, lag=1, window_len=100)[source]

Moving autocorrelation. Returned array has the same length as data. This is achieved by assuming a special kind of periodicity: end of data is prepended to the beginning. You might be better off ignoring the first window_len elements.

Parameters:
data1dim ndarray
lagint, optional
window_lenint, optional

window length, need i say more?

varwg.smoothing.corr(data1, data2, window_len=100)[source]

Moving correlation. Returned array has the same length as data. This is achieved by assuming a special kind of periodicity: end of data is prepended to the beginning. You might be better off ignoring the first window_len elements.

Parameters:
data11dim ndarray
data21dim ndarray

This array will be shifted back by ‘lag’ steps

window_lenint, optional

window length, need i say more?

varwg.smoothing.crosscorr(data1, data2, lag=1, window_len=100)[source]

Moving crosscorrelation. Returned array has the same length as data. This is achieved by assuming a special kind of periodicity: end of data is prepended to the beginning. You might be better off ignoring the first window_len elements.

Parameters:
data11dim ndarray
data21dim ndarray

This array will be shifted back by ‘lag’ steps

lagint, optional

0 works and gives correlations instead of crosscorrelations.

window_lenint, optional

window length, need i say more?

varwg.smoothing.max(data, window_len=10, periodic=False, l_value=None, r_value=None, loo=False, no_future=False, **kwds)[source]

Moving maximum.

Parameters:
data1d ndarray
window_lenint, optional

Length of the moving window.

periodicboolean, optional

Assumes the data is given as one period and appends window_len elements from the beginning at the end and window_len elements from the end to the beginning. When no trend exists, this nicely handles the estimation at the boundaries.

l_valuefloat, optional

Will be appended at the beginning. Might help to better estimate the first window_len elements.

r_valuefloat, optional

Will be appended at the end. Might help to better estimate the last window_len elements.

looboolean, optional

leave one out Estimates the percentile for data[t] without data[t].

no_future: boolean, optional

only use past values for smoothing

varwg.smoothing.maxdiff(data, window_len=10, periodic=False, l_value=None, r_value=None, loo=False, no_future=False, **kwds)[source]

Moving maximum difference.

Parameters:
data1d ndarray
window_lenint, optional

Length of the moving window.

periodicboolean, optional

Assumes the data is given as one period and appends window_len elements from the beginning at the end and window_len elements from the end to the beginning. When no trend exists, this nicely handles the estimation at the boundaries.

l_valuefloat, optional

Will be appended at the beginning. Might help to better estimate the first window_len elements.

r_valuefloat, optional

Will be appended at the end. Might help to better estimate the last window_len elements.

looboolean, optional

leave one out Estimates the percentile for data[t] without data[t].

no_future: boolean, optional

only use past values for smoothing

varwg.smoothing.median(data, window_len=10, periodic=False, l_value=None, r_value=None, loo=False, no_future=False, **kwds)[source]

Moving median.

Parameters:
data1d ndarray
window_lenint, optional

Length of the moving window.

periodicboolean, optional

Assumes the data is given as one period and appends window_len elements from the beginning at the end and window_len elements from the end to the beginning. When no trend exists, this nicely handles the estimation at the boundaries.

l_valuefloat, optional

Will be appended at the beginning. Might help to better estimate the first window_len elements.

r_valuefloat, optional

Will be appended at the end. Might help to better estimate the last window_len elements.

looboolean, optional

leave one out Estimates the percentile for data[t] without data[t].

no_future: boolean, optional

only use past values for smoothing

varwg.smoothing.min(data, window_len=10, periodic=False, l_value=None, r_value=None, loo=False, no_future=False, **kwds)[source]

Moving minimum.

Parameters:
data1d ndarray
window_lenint, optional

Length of the moving window.

periodicboolean, optional

Assumes the data is given as one period and appends window_len elements from the beginning at the end and window_len elements from the end to the beginning. When no trend exists, this nicely handles the estimation at the boundaries.

l_valuefloat, optional

Will be appended at the beginning. Might help to better estimate the first window_len elements.

r_valuefloat, optional

Will be appended at the end. Might help to better estimate the last window_len elements.

looboolean, optional

leave one out Estimates the percentile for data[t] without data[t].

no_future: boolean, optional

only use past values for smoothing

varwg.smoothing.mindiff(data, window_len=10, periodic=False, l_value=None, r_value=None, loo=False, no_future=False, **kwds)[source]

Moving minimum difference.

Parameters:
data1d ndarray
window_lenint, optional

Length of the moving window.

periodicboolean, optional

Assumes the data is given as one period and appends window_len elements from the beginning at the end and window_len elements from the end to the beginning. When no trend exists, this nicely handles the estimation at the boundaries.

l_valuefloat, optional

Will be appended at the beginning. Might help to better estimate the first window_len elements.

r_valuefloat, optional

Will be appended at the end. Might help to better estimate the last window_len elements.

looboolean, optional

leave one out Estimates the percentile for data[t] without data[t].

no_future: boolean, optional

only use past values for smoothing

varwg.smoothing.percentile(data, perc, window_len=10, periodic=False, l_value=None, r_value=None, loo=False, no_future=False)[source]

Moving percentile.

Parameters:
data1d ndarray
window_lenint, optional

Length of the moving window.

periodicboolean, optional

Assumes the data is given as one period and appends window_len elements from the beginning at the end and window_len elements from the end to the beginning. When no trend exists, this nicely handles the estimation at the boundaries.

l_valuefloat, optional

Will be appended at the beginning. Might help to better estimate the first window_len elements.

r_valuefloat, optional

Will be appended at the end. Might help to better estimate the last window_len elements.

looboolean, optional

leave one out Estimates the percentile for data[t] without data[t].

no_future: boolean, optional

only use past values for smoothing

varwg.smoothing.skew(data, window_len=10, window_function=<function hanning>, periodic=False, l_value=None, r_value=None, loo=False, no_future=False)[source]

Moving skew.

Parameters:
data1d ndarray
window_lenint, optional

Length of the moving window.

periodicboolean, optional

Assumes the data is given as one period and appends window_len elements from the beginning at the end and window_len elements from the end to the beginning. When no trend exists, this nicely handles the estimation at the boundaries.

l_valuefloat, optional

Will be appended at the beginning. Might help to better estimate the first window_len elements.

r_valuefloat, optional

Will be appended at the end. Might help to better estimate the last window_len elements.

looboolean, optional

leave one out Estimates the percentile for data[t] without data[t].

no_future: boolean, optional

only use past values for smoothing

ddofint, optional

Means Delta Degrees of Freedom. The divisor used in calculations is N - ddof, where N represents the number of elements. Default: 2 (passed on via **kwds)

varwg.smoothing.smooth(data, window_len=10, window_function=<function hanning>, periodic=False, l_value=None, r_value=None, loo=False, no_future=False, **kwds)[source]

1-dimensional smoothing Simplified & modified http://www.scipy.org/Cookbook/SignalSmooth

Parameters:
data1d ndarray
window_lenint, optional

Length of the moving window.

periodicboolean, optional

Assumes the data is given as one period and appends window_len elements from the beginning at the end and window_len elements from the end to the beginning. When no trend exists, this nicely handles the estimation at the boundaries.

l_valuefloat, optional

Will be appended at the beginning. Might help to better estimate the first window_len elements.

r_valuefloat, optional

Will be appended at the end. Might help to better estimate the last window_len elements.

looboolean, optional

leave one out Estimates the percentile for data[t] without data[t].

no_future: boolean, optional

only use past values for smoothing

ddofint, optional

Means Delta Degrees of Freedom. The divisor used in calculations is N - ddof, where N represents the number of elements. Default: 0 (passed on via **kwds)

varwg.smoothing.std(data, window_len=10, window_function=<function hanning>, periodic=False, l_value=None, r_value=None, loo=False, no_future=False, **kwds)[source]

Moving standard deviation.

Parameters:
data1d ndarray
window_lenint, optional

Length of the moving window.

periodicboolean, optional

Assumes the data is given as one period and appends window_len elements from the beginning at the end and window_len elements from the end to the beginning. When no trend exists, this nicely handles the estimation at the boundaries.

l_valuefloat, optional

Will be appended at the beginning. Might help to better estimate the first window_len elements.

r_valuefloat, optional

Will be appended at the end. Might help to better estimate the last window_len elements.

looboolean, optional

leave one out Estimates the percentile for data[t] without data[t].

no_future: boolean, optional

only use past values for smoothing

ddofint, optional

Means Delta Degrees of Freedom. The divisor used in calculations is N - ddof, where N represents the number of elements. Default: 1 (passed on via **kwds)

varwg.smoothing.variance(data, window_len=10, window_function=<function hanning>, periodic=False, l_value=None, r_value=None, loo=False, no_future=False, **kwds)[source]

Moving variance.

Parameters:
data1d ndarray
window_lenint, optional

Length of the moving window.

periodicboolean, optional

Assumes the data is given as one period and appends window_len elements from the beginning at the end and window_len elements from the end to the beginning. When no trend exists, this nicely handles the estimation at the boundaries.

l_valuefloat, optional

Will be appended at the beginning. Might help to better estimate the first window_len elements.

r_valuefloat, optional

Will be appended at the end. Might help to better estimate the last window_len elements.

looboolean, optional

leave one out Estimates the percentile for data[t] without data[t].

no_future: boolean, optional

only use past values for smoothing

ddofint, optional

Means Delta Degrees of Freedom. The divisor used in calculations is N - ddof, where N represents the number of elements. Default: 1 (passed on via **kwds)

varwg.times module

Time helpers and conversions (times)

Helper functions to convert arrays containing time information. All the functions with a 2 in the name provide the advertised conversions for scalar as well as for array input.

cwr2datetime

Converts a CWR-timesting into a datetime object.

cwr2str

Converts a CWR-timestring into a human-readable timestring.

cwr2unix

Converts a CWR-timestring to a unix timestamp.

datetime2doy

Extracts the day of year as a float from the given datetimes.

datetime2ordinal

Converts a datetime object into an ordinal.

datetime2str

Converts a datetime object into a human-readable string.

datetime2unix

Converts a dateime object into a unix-timestamp.

doy2datetime

Constructs a datetime object from given day of year.

iso2datetime

Converts an ISO formated time string to a datetime object.

iso2unix

Converts an ISO formated time string to a unix time stamp.

ordinal2datetime

Converts an ordinal to a datetime object.

str2datetime

Converts a human readable time string tinto a datetime object.

str2ordinal

Converts a human readable time string to an ordinal.

str2unix

Converts a time-string of the given d_format (default: "dd.mm.yyyy HH:MM:SS") into a unix-timestamp.

unix2cwr

Converts a unix-timestamp to a CWR-timestring.

unix2datetime

Convert a unix time stamp to a datetime object.

unix2ordinal

Converts a unix time stamp to an ordinal.

unix2str

Converts a unix-timestamp into a time-string of the given d_format (default: "dd.mm.yyyy HH:MM:SS").

time_part

Returns the "time_part" of a timestamp or datetimes.

time_part_sort

Groups "values" as a nested list according to the "sub_format_str" of the "timestamps".

timestamp2index

Calculates the array index for a certain time in an equidistant time-series given the reference time (where the index would be 0) and the time discretization.

index2timestamp

Calculates the ISOstring timestamp for a certain index in an equidistant time-series given the reference time (where the index would be 0) and the time discretization.

exception varwg.times.TimeParseError[source]

Bases: ValueError

varwg.times.build_diff_timestring(diff)[source]

Split time difference given in seconds into a string representation of days, hours, minutes and seconds.

varwg.times.cwr2datetime(cwr_string)[source]

Converts a CWR-timesting into a datetime object. >>> cwr2datetime(“2040001.00”) datetime.datetime(2040, 1, 1, 0, 0) >>> cwr2datetime(‘1980001’) datetime.datetime(1980, 1, 1, 0, 0)

varwg.times.cwr2str(cwr_string, d_format='%d.%m.%Y %H:%M:%S')[source]

Converts a CWR-timestring into a human-readable timestring.

>>> cwr2str("2040001.00")
'01.01.2040 00:00:00'
varwg.times.cwr2unix(cwr_string)[source]

Converts a CWR-timestring to a unix timestamp.

>>> cwr2unix("2040001.00")
2208988800.0
varwg.times.daily_ranges(dtimes, data)[source]

Daily max-min.

Parameters:
dtimessequence of datetime objects

For the time being it is assumed that time steps are equally spaced!

data1d array
Returns:
ranges1d array

length is number of days in dtimes.

varwg.times.date2jdn(dates)[source]

Converts date object to julian day number (without hours!)

varwg.times.datetime2cwr(dt)[source]

Converts a datetime object into a CWR/Julian timestamp.

>>> import datetime
>>> datetime2cwr(datetime.datetime(2040, 1, 1, 12, 30, 30, 0))
2040001.5211805555
varwg.times.datetime2cwr_old(dt)[source]
varwg.times.datetime2doy(dt)[source]

Extracts the day of year as a float from the given datetimes.

>>> import datetime
>>> dt = datetime.datetime(2040, 1, 1, 12, 30, 30, 500000)
>>> datetime2doy(dt)
1.5211863425925927
varwg.times.datetime2hour(dt)[source]

Extracts the hour of a day as a float from the given datetimes.

>>> import datetime
>>> dt = datetime.datetime(2040, 1, 1, 12, 30)
>>> datetime2hour(dt)
12.5
varwg.times.datetime2ordinal(dt)[source]

Converts a datetime object into an ordinal.

>>> import datetime
>>> dt = datetime.datetime(2040, 1, 1, 0, 0, 0, 500000)
>>> datetime2ordinal(dt)
744730
>>> datetime2ordinal(datetime.datetime(1, 1, 1, 0, 0))
1
varwg.times.datetime2str(dt, d_format='%d.%m.%Y %H:%M:%S')[source]

Converts a datetime object into a human-readable string.

>>> import datetime
>>> datetime2str(datetime.datetime(2040, 1, 1, 0, 0, 0, 500000))
'01.01.2040 00:00:00'
varwg.times.datetime2unix(dt)[source]

Converts a dateime object into a unix-timestamp.

>>> import datetime
>>> dt = datetime.datetime(2040, 1, 1, 0, 0, 0, 500000)
>>> datetime2unix(dt)
2208988800.5
>>> datetime2unix(datetime.datetime(1970, 1, 1, 0, 0))
0.0
varwg.times.datetimefromisoformat(ts, fmt='%Y-%m-%dT%H:%M:%S')[source]
varwg.times.doy2datetime(doy, year=2000)[source]

Constructs a datetime object from given day of year.

Parameters:
yearint, optional

The year of the day of the year.

Examples

>>> import datetime, numpy
>>> doy2datetime(1.5211863425925927, 2040)
datetime.datetime(2040, 1, 1, 12, 30, 30, 500000)
>>> doy2datetime(numpy.arange(1,3))
array([datetime.datetime(2000, 1, 1, 0, 0),
       datetime.datetime(2000, 1, 2, 0, 0)], dtype=object)
varwg.times.doy_distance(doy1, doy2)[source]

Examples

>>> import numpy as np
>>> doy_distance(0, np.array([364, 0, 1]))
array([ 1.,  0.,  1.])
varwg.times.dy2datetime(dy)[source]

Converts a DYRESM float time (a.k.a. julian date) into a datetime object.

>>> dy2datetime(2451544.5)
datetime.datetime(2000, 1, 1, 0, 0)
>>> dy2datetime(2456462.4375)
datetime.datetime(2013, 6, 18, 22, 30)
varwg.times.expand_timeseries(timestamps, repeats=4, values=None)[source]

Interpolates timestamps and repeats according values. Assumes constant length of time-step.

Examples

>>> import numpy as np
>>> timestamps = str2unix(["01.01.1980 00:00:00", "02.01.1980 00:00:00"])
>>> values = np.array([1, 2])
>>> timestamps, values = expand_timeseries(timestamps, 2, values)
>>> print (unix2str(timestamps), values)
['01.01.1980 00:00:00' '01.01.1980 12:00:00' '02.01.1980 00:00:00'
 '02.01.1980 12:00:00'] [1 1 2 2]
varwg.times.feb29_mask(dtimes)[source]

Returns a mask indicating the location of the additional day in the leap years.

Should help you to get rid off those buggers.

Parameters:
dtimesndarray
Returns:
maskboolean ndarray
varwg.times.hour_distance(hour1, hour2)[source]
varwg.times.index2timestamp(idx, dt, refts, **kwargs)[source]

Calculates the ISOstring timestamp for a certain index in an equidistant time-series given the reference time (where the index would be 0) and the time discretization. If any of the input parameters contains timezone information, all others also need to contain timezone information.

Parameters:
idxstr or datetime-object

The timestamp to determine the index for If it is a string, it will be converted to datetime using the function _datetimefromisoformat Formatting keywords may be passed to this function

dtstr or timedelta object

The discretization of the time series (the amount of time that elapsed between indices) If used as a string, it needs to be given in the format “keyword1=value1,keyword2=value2”. Keywords must be understood by the timedelta constructor (like days, hours, minutes, seconds) and the values may only be integers.

reftsstr or datetime-object

The timestamp to determine the index for If it is a string, it will be converted to datetime using the function _datetimefromisoformat Formatting keywords may be passed to this function

Returns:
ISO-timestampstring

The ISO-timestamp of a discrete time series array of the given parameters in this format: ‘%Y-%m-%dT%H:%M:%S’

Examples

>>> index2timestamp(25637, 'seconds=10', '2010-09-25T00:00:10')
'2010-09-27T23:13:00'
>>> index2timestamp(365, 'days=1', '2010-09-25T00:00:10')
'2011-09-25T00:00:10'
varwg.times.iso2datetime(iso)[source]

Converts an ISO formated time string to a datetime object.

varwg.times.iso2unix(iso)[source]

Converts an ISO formated time string to a unix time stamp.

varwg.times.isoformat2unix(ts)[source]
varwg.times.ordinal2datetime(ord_)[source]

Converts an ordinal to a datetime object.

>>> ordinal2datetime(744730)
datetime.datetime(2040, 1, 1, 0, 0)
varwg.times.periodic_distance(x1, x2, period)[source]

Examples

>>> periodic_distance(0, 23, 24)
array(1.0)
varwg.times.regularize(values, dtimes, nan=False, main_diff=None)[source]

Regularize an irregular time series by linear interpolation. The time interval is guessed from the most frequent interval in times.

>>> dtimes = datetime(2000, 1, 1) + np.arange(4) * timedelta(hours=1)
>>> dtimes[1] += timedelta(minutes=30)
>>> values = np.arange(4.)
>>> values[1] = 1.5
>>> values
array([ 0. ,  1.5,  2. ,  3. ])
>>> dtimes
array([datetime.datetime(2000, 1, 1, 0, 0),
       datetime.datetime(2000, 1, 1, 1, 30),
       datetime.datetime(2000, 1, 1, 2, 0),
       datetime.datetime(2000, 1, 1, 3, 0)], dtype=object)
>>> regularize(values, dtimes)
(array([ 0.,  1.,  2.,  3.]), array([datetime.datetime(2000, 1, 1, 0, 0),
       datetime.datetime(2000, 1, 1, 1, 0),
       datetime.datetime(2000, 1, 1, 2, 0),
       datetime.datetime(2000, 1, 1, 3, 0)], dtype=object))
varwg.times.str2datetime(str_, d_format='%d.%m.%Y %H:%M:%S')[source]

Converts a human readable time string tinto a datetime object.

>>> str2datetime("01.01.2040 00:00:00")
datetime.datetime(2040, 1, 1, 0, 0)
varwg.times.str2ordinal(str_, d_format='%d.%m.%Y %H:%M:%S')[source]

Converts a human readable time string to an ordinal.

>>> str2ordinal("01.01.2040 00:00:00")
744730
>>> str2ordinal("01.01.0001 00:00:00")
1
varwg.times.str2unix(str_, d_format='%d.%m.%Y %H:%M:%S')[source]

Converts a time-string of the given d_format (default: “dd.mm.yyyy HH:MM:SS”) into a unix-timestamp.

>>> str2unix("01.01.2040 00:00:00")
2208988800.0
>>> str2unix("01.01.1970 00:00:00")
0.0
varwg.times.time_part(timestamps_or_datetimes, sub_format_str)[source]

Returns the “time_part” of a timestamp or datetimes. The time_part has to be convertible into an integer. This is useful to group values according to months, weeks and so on.

varwg.times.time_part_(datetimes, date_part)[source]
varwg.times.time_part_sort(timestamps, values, sub_format_str)[source]

Groups “values” as a nested list according to the “sub_format_str” of the “timestamps”.

>>> dtimes = datetime(2000, 1, 1) + np.arange(4) * timedelta(days=16)
>>> values = np.arange(4)
>>> time_part_sort(dtimes, values, "%m")
([1, 2], [array([0, 1]), array([2, 3])])
varwg.times.time_part_sort_(datetimes, values, date_part)[source]
varwg.times.timedelta2seconds(dt)[source]
varwg.times.timedelta2slice(delta, dt, offset=0, step=None)[source]
varwg.times.timestamp2index(ts, dt, refts, **kwargs)[source]

Calculates the array index for a certain time in an equidistant time-series given the reference time (where the index would be 0) and the time discretization. If any of the input parameters contains timezone information, all others also need to contain timezone information.

Parameters:
tsstr or datetime-object

The timestamp to determine the index for If it is a string, it will be converted to datetime using the function _datetimefromisoformat Formatting keywords may be passed to this function

dtstr or timedelta object

The discretization of the time series (the amount of time that elapsed between indices) If used as a string, it needs to be given in the format “keyword1=value1,keyword2=value2”. Keywords must be understood by the timedelta constructor (like days, hours, minutes, seconds) and the values may only be integers.

reftsstr or datetime-object

The timestamp to determine the index for If it is a string, it will be converted to datetime using the function _datetimefromisoformat Formatting keywords may be passed to this function

Returns:
indexinteger

The index of a discrete time series array of the given parameters

Examples

>>> timestr1, timestr2 = '2008-06-01T00:00:00', '2007-01-01T00:00:00'
>>> timestamp2index(timestr1, 'minutes=5', timestr2)
148896
>>> timestamp2index(timestr1, 'hours=1,minutes=5',timestr2)
11453
>>> timestamp2index(timestr1, timedelta(hours=1, minutes=5), timestr2)
11453
varwg.times.timestamps2slice(startts=None, endts=None, dt=None, refts=None, step=None)[source]

Ask Thomas.

>>> timestamps2slice('2010-03-01T23:54:00', '2010-03-07T23:52:00',                          timedelta(days=1))
slice(0, 5, None)
>>> timestamps2slice('2010-03-01T23:54:00', '2010-03-07T23:52:00',                          timedelta(days=1), '2010-03-04T00:00:00')
slice(-2, 3, None)
>>> timestamps2slice('2010-03-01T23:54:00', '2010-03-07T23:52:00',                          timedelta(days=1), '2010-01-04T00:00:00')
slice(56, 61, None)
varwg.times.unix2cwr(timestamp)[source]

Converts a unix-timestamp to a CWR-timestring.

>>> unix2cwr(2208988800.0)
2040001.0
varwg.times.unix2datetime(timestamp)[source]

Convert a unix time stamp to a datetime object.

>>> import datetime
>>> unix2datetime(2208988800.5)
datetime.datetime(2040, 1, 1, 0, 0, 0, 500000)
>>> unix2datetime(0)
datetime.datetime(1970, 1, 1, 0, 0)
varwg.times.unix2ordinal(timestamp)[source]

Converts a unix time stamp to an ordinal.

>>> unix2ordinal(2208988800.0)
744730
varwg.times.unix2str(timestamp, d_format='%d.%m.%Y %H:%M:%S')[source]

Converts a unix-timestamp into a time-string of the given d_format (default: “dd.mm.yyyy HH:MM:SS”).

>>> unix2str(2208988800.5)
'01.01.2040 00:00:00'
>>> unix2str(0)
'01.01.1970 00:00:00'
varwg.times.xls2datetime(xldate, datemode=0)[source]

Here’s the bare-knuckle no-seat-belts use-at-own-risk version posted by John Machin in http://stackoverflow.com datemode: 0 for 1900-based, 1 for 1904-based

Module contents