Completed
Pull Request — master (#881)
by
unknown
01:24
created

zipline.utils.make_datetime64ns()   A

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 7
rs 9.4286
1
"""
2
Utilities for working with numpy arrays.
3
"""
4
from numpy import dtype, datetime64
5
from numpy.lib.stride_tricks import as_strided
6
7
float64_dtype = dtype('float64')
8
datetime64ns_dtype = dtype('datetime64[ns]')
9
10
11
def make_datetime64ns(value):
12
    """
13
    Wrapper around the datetime64 constructor that uses 'ns' as a unit.
14
    """
15
    # This can't just be a partial because datetime64 is a C function and
16
    # doesn't accept kwargs.
17
    return datetime64(value, 'ns')
18
19
20
def repeat_first_axis(array, count):
21
    """
22
    Restride `array` to repeat `count` times along the first axis.
23
24
    Parameters
25
    ----------
26
    array : np.array
27
        The array to restride.
28
    count : int
29
        Number of times to repeat `array`.
30
31
    Returns
32
    -------
33
    result : array
34
        Array of shape (count,) + array.shape, composed of `array` repeated
35
        `count` times along the first axis.
36
37
    Example
38
    -------
39
    >>> from numpy import arange
40
    >>> a = arange(3); a
41
    array([0, 1, 2])
42
    >>> repeat_first_axis(a, 2)
43
    array([[0, 1, 2],
44
           [0, 1, 2]])
45
    >>> repeat_first_axis(a, 4)
46
    array([[0, 1, 2],
47
           [0, 1, 2],
48
           [0, 1, 2],
49
           [0, 1, 2]])
50
51
    Notes
52
    ----
53
    The resulting array will share memory with `array`.  If you need to assign
54
    to the input or output, you should probably make a copy first.
55
56
    See Also
57
    --------
58
    repeat_last_axis
59
    """
60
    return as_strided(array, (count,) + array.shape, (0,) + array.strides)
61
62
63
def repeat_last_axis(array, count):
64
    """
65
    Restride `array` to repeat `count` times along the last axis.
66
67
    Parameters
68
    ----------
69
    array : np.array
70
        The array to restride.
71
    count : int
72
        Number of times to repeat `array`.
73
74
    Returns
75
    -------
76
    result : array
77
        Array of shape array.shape + (count,) composed of `array` repeated
78
        `count` times along the last axis.
79
80
    Example
81
    -------
82
    >>> from numpy import arange
83
    >>> a = arange(3); a
84
    array([0, 1, 2])
85
    >>> repeat_last_axis(a, 2)
86
    array([[0, 0],
87
           [1, 1],
88
           [2, 2]])
89
    >>> repeat_last_axis(a, 4)
90
    array([[0, 0, 0, 0],
91
           [1, 1, 1, 1],
92
           [2, 2, 2, 2]])
93
94
    Notes
95
    ----
96
    The resulting array will share memory with `array`.  If you need to assign
97
    to the input or output, you should probably make a copy first.
98
99
    See Also
100
    --------
101
    repeat_last_axis
102
    """
103
    return as_strided(array, array.shape + (count,), array.strides + (0,))
104