1
- from .pandas_vb_common import *
1
+ import numpy as np
2
+ import pandas .util .testing as tm
3
+ from pandas import DataFrame , Series , MultiIndex , Timestamp , date_range
2
4
try :
3
- from pandas .tseries . offsets import *
5
+ from pandas .tseries import offsets
4
6
except :
5
7
from pandas .core .datetools import *
6
8
7
9
8
- #----------------------------------------------------------------------
10
+ # ----------------------------------------------------------------------
9
11
# Creation from nested dict
10
12
11
13
class FromDicts (object ):
14
+
12
15
goal_time = 0.2
13
16
14
17
def setup (self ):
15
- (N , K ) = (5000 , 50 )
18
+ np .random .seed (1234 )
19
+ N , K = 5000 , 50
16
20
self .index = tm .makeStringIndex (N )
17
21
self .columns = tm .makeStringIndex (K )
18
- self .frame = DataFrame (np .random .randn (N , K ), index = self .index , columns = self .columns )
19
- try :
20
- self .data = self .frame .to_dict ()
21
- except :
22
- self .data = self .frame .toDict ()
22
+ self .frame = DataFrame (np .random .randn (N , K ),
23
+ index = self .index ,
24
+ columns = self .columns )
25
+ self .data = self .frame .to_dict ()
23
26
self .some_dict = list (self .data .values ())[0 ]
24
- self .dict_list = [dict (zip (self .columns , row )) for row in self .frame .values ]
25
-
27
+ self .dict_list = self .frame .to_dict (orient = 'records' )
26
28
self .data2 = {i : {j : float (j ) for j in range (100 )}
27
29
for i in range (2000 )}
28
30
29
-
30
31
def time_frame_ctor_list_of_dict (self ):
31
32
DataFrame (self .dict_list )
32
33
@@ -38,38 +39,21 @@ def time_series_ctor_from_dict(self):
38
39
39
40
def time_frame_ctor_nested_dict_int64 (self ):
40
41
# nested dict, integer indexes, regression described in #621
41
- DataFrame (self .data )
42
+ DataFrame (self .data2 )
42
43
43
44
44
45
# from a mi-series
45
46
46
- class frame_from_series (object ):
47
+ class FromSeries (object ):
47
48
goal_time = 0.2
48
49
49
50
def setup (self ):
50
- self .mi = MultiIndex .from_tuples ([( x , y ) for x in range (100 ) for y in range (100 )])
51
- self .s = Series (randn (10000 ), index = self .mi )
51
+ self .mi = MultiIndex .from_product ([ range (100 ), range (100 )])
52
+ self .s = Series (np . random . randn (10000 ), index = self .mi )
52
53
53
54
def time_frame_from_mi_series (self ):
54
55
DataFrame (self .s )
55
56
56
-
57
- #----------------------------------------------------------------------
58
- # get_numeric_data
59
-
60
- class frame_get_numeric_data (object ):
61
- goal_time = 0.2
62
-
63
- def setup (self ):
64
- self .df = DataFrame (randn (10000 , 25 ))
65
- self .df ['foo' ] = 'bar'
66
- self .df ['bar' ] = 'baz'
67
- self .df = self .df .consolidate ()
68
-
69
- def time_frame_get_numeric_data (self ):
70
- self .df ._get_numeric_data ()
71
-
72
-
73
57
# ----------------------------------------------------------------------
74
58
# From dict with DatetimeIndex with all offsets
75
59
@@ -84,13 +68,15 @@ def get_period_count(start_date, off):
84
68
if (ten_offsets_in_days == 0 ):
85
69
return 1000
86
70
else :
87
- return min ((9 * ((Timestamp .max - start_date ).days // ten_offsets_in_days )), 1000 )
71
+ periods = 9 * (Timestamp .max - start_date ).days // ten_offsets_in_days
72
+ return min (periods , 1000 )
88
73
89
74
90
75
def get_index_for_offset (off ):
91
76
start_date = Timestamp ('1/1/1900' )
92
- return date_range (start_date , periods = min (1000 , get_period_count (
93
- start_date , off )), freq = off )
77
+ return date_range (start_date ,
78
+ periods = get_period_count (start_date , off ),
79
+ freq = off )
94
80
95
81
96
82
all_offsets = offsets .__all__
@@ -100,21 +86,23 @@ def get_index_for_offset(off):
100
86
all_offsets .extend ([off + '_1' , off + '_2' ])
101
87
102
88
103
- class FrameConstructorDTIndexFromOffsets (object ):
89
+ class FromDictwithTimestampOffsets (object ):
104
90
105
91
params = [all_offsets , [1 , 2 ]]
106
92
param_names = ['offset' , 'n_steps' ]
107
93
108
94
offset_kwargs = {'WeekOfMonth' : {'weekday' : 1 , 'week' : 1 },
109
95
'LastWeekOfMonth' : {'weekday' : 1 , 'week' : 1 },
110
96
'FY5253' : {'startingMonth' : 1 , 'weekday' : 1 },
111
- 'FY5253Quarter' : {'qtr_with_extra_week' : 1 , 'startingMonth' : 1 , 'weekday' : 1 }}
97
+ 'FY5253Quarter' : {'qtr_with_extra_week' : 1 ,
98
+ 'startingMonth' : 1 ,
99
+ 'weekday' : 1 }}
112
100
113
101
offset_extra_cases = {'FY5253' : {'variation' : ['nearest' , 'last' ]},
114
102
'FY5253Quarter' : {'variation' : ['nearest' , 'last' ]}}
115
103
116
104
def setup (self , offset , n_steps ):
117
-
105
+ np . random . seed ( 1234 )
118
106
extra = False
119
107
if offset .endswith ("_" , None , - 1 ):
120
108
extra = int (offset [- 1 ])
@@ -127,12 +115,12 @@ def setup(self, offset, n_steps):
127
115
if extra :
128
116
extras = self .offset_extra_cases [offset ]
129
117
for extra_arg in extras :
130
- kwargs [extra_arg ] = extras [extra_arg ][extra - 1 ]
118
+ kwargs [extra_arg ] = extras [extra_arg ][extra - 1 ]
131
119
132
120
offset = getattr (offsets , offset )
133
121
self .idx = get_index_for_offset (offset (n_steps , ** kwargs ))
134
122
self .df = DataFrame (np .random .randn (len (self .idx ), 10 ), index = self .idx )
135
- self .d = dict ( self .df .items () )
123
+ self .d = self .df .to_dict ( )
136
124
137
125
def time_frame_ctor (self , offset , n_steps ):
138
126
DataFrame (self .d )
0 commit comments