@@ -90,21 +90,17 @@ def read_bsrn(filename):
90
90
# Read file and store the starting line number for each logical record (LR)
91
91
line_no_dict = {}
92
92
if str (filename ).endswith ('.gz' ): # check if file is a gzipped (.gz) file
93
- with gzip .open (filename , 'rt' ) as f :
94
- for num , line in enumerate (f ):
95
- if num == 1 : # Get month and year from the 2nd line
96
- start_date = pd .Timestamp (year = int (line [7 :11 ]),
97
- month = int (line [3 :6 ]), day = 1 )
98
- if line .startswith ('*' ): # Find start of all logical records
99
- line_no_dict [line [2 :6 ]] = num # key is 4 digit LR number
93
+ open_func , mode = gzip .open , 'rt'
100
94
else :
101
- with open (filename , 'r' ) as f :
102
- for num , line in enumerate (f ):
103
- if num == 1 : # Get month and year from the 2nd line
104
- start_date = pd .Timestamp (year = int (line [7 :11 ]),
105
- month = int (line [3 :6 ]), day = 1 )
106
- if line .startswith ('*' ): # Find start of all logical records
107
- line_no_dict [line [2 :6 ]] = num
95
+ open_func , mode = open , 'r'
96
+ with open_func (filename , mode ) as f :
97
+ for num , line in enumerate (f ):
98
+ if num == 1 : # Get month and year from the 2nd line
99
+ start_date = pd .Timestamp (year = int (line [7 :11 ]),
100
+ month = int (line [3 :6 ]), day = 1 ,
101
+ tz = 'UTC' ) # BSRN timestamps are UTC
102
+ if line .startswith ('*' ): # Find start of all logical records
103
+ line_no_dict [line [2 :6 ]] = num # key is 4 digit LR number
108
104
109
105
# Determine start and end line of logical record LR0100 to be parsed
110
106
start_row = line_no_dict ['0100' ] + 1 # Start line number
@@ -133,14 +129,9 @@ def read_bsrn(filename):
133
129
data ['day' ] = data ['day' ].astype ('Int64' )
134
130
data ['minute' ] = data ['minute' ].astype ('Int64' )
135
131
136
- # Set datetime index and localize to UTC
132
+ # Set datetime index
137
133
data .index = (start_date
138
134
+ pd .to_timedelta (data ['day' ]- 1 , unit = 'd' )
139
135
+ pd .to_timedelta (data ['minute' ], unit = 'min' ))
140
136
141
- try :
142
- data .index = data .index .tz_localize ('UTC' ) # BSRN timestamps are UTC
143
- except TypeError :
144
- pass
145
-
146
137
return data
0 commit comments