def _stata_elapsed_date_to_datetime_vec(dates, fmt):
MIN_YEAR, MAX_YEAR = Timestamp.min.year, Timestamp.max.year<EOL>MAX_DAY_DELTA = (Timestamp.max - datetime.datetime(<NUM_LIT>, <NUM_LIT:1>, <NUM_LIT:1>)).days<EOL>MIN_DAY_DELTA = (Timestamp.min - datetime.datetime(<NUM_LIT>, <NUM_LIT:1>, <NUM_LIT:1>)).days<EOL>MIN_MS_DELTA = MIN_DAY_DELTA * <NUM_LIT> * <NUM_LIT> * <NUM_LIT:1000><EOL>MAX_MS_DELTA = MAX_DAY_DELTA * <NUM_LIT> * <NUM_LIT> * <NUM_LIT:1000><EOL>def convert_year_month_safe(year, month):<EOL><INDENT>"""<STR_LIT>"""<EOL>if year.max() < MAX_YEAR and year.min() > MIN_YEAR:<EOL><INDENT>return to_datetime(<NUM_LIT:100> * year + month, format='<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>index = getattr(year, '<STR_LIT:index>', None)<EOL>return Series(<EOL>[datetime.datetime(y, m, <NUM_LIT:1>) for y, m in zip(year, month)],<EOL>index=index)<EOL><DEDENT><DEDENT>def convert_year_days_safe(year, days):<EOL><INDENT>"""<STR_LIT>"""<EOL>if year.max() < (MAX_YEAR - <NUM_LIT:1>) and year.min() > MIN_YEAR:<EOL><INDENT>return (to_datetime(year, format='<STR_LIT>') +<EOL>to_timedelta(days, unit='<STR_LIT:d>'))<EOL><DEDENT>else:<EOL><INDENT>index = getattr(year, '<STR_LIT:index>', None)<EOL>value = [datetime.datetime(y, <NUM_LIT:1>, <NUM_LIT:1>) + relativedelta(days=int(d))<EOL>for y, d in zip(year, days)]<EOL>return Series(value, index=index)<EOL><DEDENT><DEDENT>def convert_delta_safe(base, deltas, unit):<EOL><INDENT>"""<STR_LIT>"""<EOL>index = getattr(deltas, '<STR_LIT:index>', None)<EOL>if unit == '<STR_LIT:d>':<EOL><INDENT>if deltas.max() > MAX_DAY_DELTA or deltas.min() < MIN_DAY_DELTA:<EOL><INDENT>values = [base + relativedelta(days=int(d)) for d in deltas]<EOL>return Series(values, index=index)<EOL><DEDENT><DEDENT>elif unit == '<STR_LIT>':<EOL><INDENT>if deltas.max() > MAX_MS_DELTA or deltas.min() < MIN_MS_DELTA:<EOL><INDENT>values = [base + relativedelta(microseconds=(int(d) * <NUM_LIT:1000>))<EOL>for d in deltas]<EOL>return Series(values, index=index)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>base = to_datetime(base)<EOL>deltas = to_timedelta(deltas, unit=unit)<EOL>return base + deltas<EOL><DEDENT>bad_locs = np.isnan(dates)<EOL>has_bad_values = False<EOL>if bad_locs.any():<EOL><INDENT>has_bad_values = True<EOL>data_col = Series(dates)<EOL>data_col[bad_locs] = <NUM_LIT:1.0>  <EOL><DEDENT>dates = dates.astype(np.int64)<EOL>if fmt.startswith(("<STR_LIT>", "<STR_LIT>")):  <EOL><INDENT>base = stata_epoch<EOL>ms = dates<EOL>conv_dates = convert_delta_safe(base, ms, '<STR_LIT>')<EOL><DEDENT>elif fmt.startswith(("<STR_LIT>", "<STR_LIT>")):<EOL><INDENT>warnings.warn("<STR_LIT>"<EOL>"<STR_LIT>")<EOL>conv_dates = Series(dates, dtype=np.object)<EOL>if has_bad_values:<EOL><INDENT>conv_dates[bad_locs] = NaT<EOL><DEDENT>return conv_dates<EOL><DEDENT>elif fmt.startswith(("<STR_LIT>", "<STR_LIT>", "<STR_LIT>", "<STR_LIT:d>")):<EOL><INDENT>base = stata_epoch<EOL>days = dates<EOL>conv_dates = convert_delta_safe(base, days, '<STR_LIT:d>')<EOL><DEDENT>elif fmt.startswith(("<STR_LIT>", "<STR_LIT>")):<EOL><INDENT>year = stata_epoch.year + dates // <NUM_LIT><EOL>days = (dates % <NUM_LIT>) * <NUM_LIT:7><EOL>conv_dates = convert_year_days_safe(year, days)<EOL><DEDENT>elif fmt.startswith(("<STR_LIT>", "<STR_LIT>")):  <EOL><INDENT>year = stata_epoch.year + dates // <NUM_LIT:12><EOL>month = (dates % <NUM_LIT:12>) + <NUM_LIT:1><EOL>conv_dates = convert_year_month_safe(year, month)<EOL><DEDENT>elif fmt.startswith(("<STR_LIT>", "<STR_LIT>")):  <EOL><INDENT>year = stata_epoch.year + dates // <NUM_LIT:4><EOL>month = (dates % <NUM_LIT:4>) * <NUM_LIT:3> + <NUM_LIT:1><EOL>conv_dates = convert_year_month_safe(year, month)<EOL><DEDENT>elif fmt.startswith(("<STR_LIT>", "<STR_LIT>")):  <EOL><INDENT>year = stata_epoch.year + dates // <NUM_LIT:2><EOL>month = (dates % <NUM_LIT:2>) * <NUM_LIT:6> + <NUM_LIT:1><EOL>conv_dates = convert_year_month_safe(year, month)<EOL><DEDENT>elif fmt.startswith(("<STR_LIT>", "<STR_LIT>")):  <EOL><INDENT>year = dates<EOL>month = np.ones_like(dates)<EOL>conv_dates = convert_year_month_safe(year, month)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>".format(fmt=fmt))<EOL><DEDENT>if has_bad_values:  <EOL><INDENT>conv_dates[bad_locs] = NaT<EOL><DEDENT>return conv_dates<EOL>
Convert from SIF to datetime. http://www.stata.com/help.cgi?datetime

Parameters
----------
dates : Series
    The Stata Internal Format date to convert to datetime according to fmt
fmt : str
    The format to convert to. Can be, tc, td, tw, tm, tq, th, ty
    Returns

Returns
-------
converted : Series
    The converted dates

Examples
--------
>>> dates = pd.Series([52])
>>> _stata_elapsed_date_to_datetime_vec(dates , "%tw")
0   1961-01-01
dtype: datetime64[ns]

Notes
-----
datetime/c - tc
    milliseconds since 01jan1960 00:00:00.000, assuming 86,400 s/day
datetime/C - tC - NOT IMPLEMENTED
    milliseconds since 01jan1960 00:00:00.000, adjusted for leap seconds
date - td
    days since 01jan1960 (01jan1960 = 0)
weekly date - tw
    weeks since 1960w1
    This assumes 52 weeks in a year, then adds 7 * remainder of the weeks.
    The datetime value is the start of the week in terms of days in the
    year, not ISO calendar weeks.
monthly date - tm
    months since 1960m1
quarterly date - tq
    quarters since 1960q1
half-yearly date - th
    half-years since 1960h1 yearly
date - ty
    years since 0000

If you don't have pandas with datetime support, then you can't do
milliseconds accurately.