@@ -306,39 +306,42 @@ def convert_delta_safe(base, deltas, unit):
306
306
data_col [bad_locs ] = 1.0 # Replace with NaT
307
307
dates = dates .astype (np .int64 )
308
308
309
- if fmt in [ "%tc" , "tc" ] : # Delta ms relative to base
309
+ if fmt . startswith (( "%tc" , "tc" )) : # Delta ms relative to base
310
310
base = stata_epoch
311
311
ms = dates
312
312
conv_dates = convert_delta_safe (base , ms , 'ms' )
313
- elif fmt in [ "%tC" , "tC" ] :
313
+ elif fmt . startswith (( "%tC" , "tC" )) :
314
314
from warnings import warn
315
315
316
316
warn ("Encountered %tC format. Leaving in Stata Internal Format." )
317
317
conv_dates = Series (dates , dtype = np .object )
318
318
if has_bad_values :
319
319
conv_dates [bad_locs ] = pd .NaT
320
320
return conv_dates
321
- elif fmt in ["%td" , "td" , "%d" , "d" ]: # Delta days relative to base
321
+ # Delta days relative to base
322
+ elif fmt .startswith (("%td" , "td" , "%d" , "d" )):
322
323
base = stata_epoch
323
324
days = dates
324
325
conv_dates = convert_delta_safe (base , days , 'd' )
325
- elif fmt in ["%tw" , "tw" ]: # does not count leap days - 7 days is a week
326
+ # does not count leap days - 7 days is a week.
327
+ # 52nd week may have more than 7 days
328
+ elif fmt .startswith (("%tw" , "tw" )):
326
329
year = stata_epoch .year + dates // 52
327
330
days = (dates % 52 ) * 7
328
331
conv_dates = convert_year_days_safe (year , days )
329
- elif fmt in [ "%tm" , "tm" ] : # Delta months relative to base
332
+ elif fmt . startswith (( "%tm" , "tm" )) : # Delta months relative to base
330
333
year = stata_epoch .year + dates // 12
331
334
month = (dates % 12 ) + 1
332
335
conv_dates = convert_year_month_safe (year , month )
333
- elif fmt in [ "%tq" , "tq" ] : # Delta quarters relative to base
336
+ elif fmt . startswith (( "%tq" , "tq" )) : # Delta quarters relative to base
334
337
year = stata_epoch .year + dates // 4
335
338
month = (dates % 4 ) * 3 + 1
336
339
conv_dates = convert_year_month_safe (year , month )
337
- elif fmt in [ "%th" , "th" ] : # Delta half-years relative to base
340
+ elif fmt . startswith (( "%th" , "th" )) : # Delta half-years relative to base
338
341
year = stata_epoch .year + dates // 2
339
342
month = (dates % 2 ) * 6 + 1
340
343
conv_dates = convert_year_month_safe (year , month )
341
- elif fmt in [ "%ty" , "ty" ] : # Years -- not delta
344
+ elif fmt . startswith (( "%ty" , "ty" )) : # Years -- not delta
342
345
year = dates
343
346
month = np .ones_like (dates )
344
347
conv_dates = convert_year_month_safe (year , month )
@@ -1029,10 +1032,6 @@ def _read_header(self):
1029
1032
# calculate size of a data record
1030
1033
self .col_sizes = lmap (lambda x : self ._calcsize (x ), self .typlist )
1031
1034
1032
- # remove format details from %td
1033
- self .fmtlist = ["%td" if x .startswith ("%td" ) else x
1034
- for x in self .fmtlist ]
1035
-
1036
1035
def _read_new_header (self , first_char ):
1037
1036
# The first part of the header is common to 117 and 118.
1038
1037
self .path_or_buf .read (27 ) # stata_dta><header><release>
@@ -1578,7 +1577,8 @@ def read(self, nrows=None, convert_dates=None,
1578
1577
self ._do_convert_missing (data , convert_missing )
1579
1578
1580
1579
if convert_dates :
1581
- cols = np .where (lmap (lambda x : x in _date_formats ,
1580
+ cols = np .where (lmap (lambda x : any (x .startswith (fmt )
1581
+ for fmt in _date_formats ),
1582
1582
self .fmtlist ))[0 ]
1583
1583
for i in cols :
1584
1584
col = data .columns [i ]
0 commit comments