~ubuntu-branches/ubuntu/raring/pandas/raring

« back to all changes in this revision

Viewing changes to pandas/src/datetime.pyx

  • Committer: Package Import Robot
  • Author(s): Yaroslav Halchenko
  • Date: 2012-07-22 20:13:16 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20120722201316-6v4zbottvreomrz7
Tags: 0.8.1-1
* Primarily a bugfix upstream release.
* up_tag_yahoo_test_requiring_network patch removed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
632
632
 
633
633
    if PyUnicode_Check(val):
634
634
        val = PyUnicode_AsASCIIString(val);
635
 
    result = parse_iso_8601_datetime(val, len(val), PANDAS_FR_ns, NPY_UNSAFE_CASTING,
 
635
 
 
636
    result = parse_iso_8601_datetime(val, len(val), PANDAS_FR_ns,
 
637
                                     NPY_UNSAFE_CASTING,
636
638
                                     dts, &islocal, &out_bestunit, &special)
637
639
    if result == -1:
638
640
        raise ValueError('Unable to parse %s' % str(val))
639
641
 
640
 
def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False):
 
642
def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False,
 
643
                      format=None, utc=None):
641
644
    cdef:
642
645
        Py_ssize_t i, n = len(values)
643
646
        object val
644
647
        ndarray[int64_t] iresult
645
648
        ndarray[object] oresult
646
649
        pandas_datetimestruct dts
 
650
        bint utc_convert = bool(utc)
 
651
        _TSObject _ts
647
652
 
648
653
    from dateutil.parser import parse
649
654
 
655
660
            if util._checknull(val):
656
661
                iresult[i] = iNaT
657
662
            elif PyDateTime_Check(val):
658
 
                iresult[i] = _pydatetime_to_dts(val, &dts)
659
 
                _check_dts_bounds(iresult[i], &dts)
 
663
                if val.tzinfo is not None:
 
664
                    if utc_convert:
 
665
                        _ts = convert_to_tsobject(val)
 
666
                        iresult[i] = _ts.value
 
667
                        _check_dts_bounds(iresult[i], &_ts.dts)
 
668
                    else:
 
669
                        raise ValueError('Tz-aware datetime.datetime cannot '
 
670
                                         'be converted to datetime64 unless '
 
671
                                         'utc=True')
 
672
                else:
 
673
                    iresult[i] = _pydatetime_to_dts(val, &dts)
 
674
                    _check_dts_bounds(iresult[i], &dts)
660
675
            elif PyDate_Check(val):
661
676
                iresult[i] = _date_to_datetime64(val, &dts)
662
677
                _check_dts_bounds(iresult[i], &dts)
668
683
                if len(val) == 0:
669
684
                    iresult[i] = iNaT
670
685
                    continue
 
686
 
671
687
                try:
672
 
                    result[i] = parse(val, dayfirst=dayfirst)
673
 
                except Exception:
674
 
                    raise TypeError
675
 
                pandas_datetime_to_datetimestruct(iresult[i], PANDAS_FR_ns,
676
 
                                                  &dts)
 
688
                    _string_to_dts(val, &dts)
 
689
                    iresult[i] = pandas_datetimestruct_to_datetime(PANDAS_FR_ns,
 
690
                                                                   &dts)
 
691
                except ValueError:
 
692
                    try:
 
693
                        result[i] = parse(val, dayfirst=dayfirst)
 
694
                    except Exception:
 
695
                        raise TypeError
 
696
                    pandas_datetime_to_datetimestruct(iresult[i], PANDAS_FR_ns,
 
697
                                                      &dts)
677
698
                _check_dts_bounds(iresult[i], &dts)
678
699
        return result
679
700
    except TypeError:
768
789
 
769
790
try:
770
791
    import pytz
 
792
    UTC = pytz.utc
771
793
    have_pytz = True
772
794
except:
773
795
    have_pytz = False