date#

date(obj=None, *args, start_date=None, readformat=None, to='date', as_date=None, outformat=None, **kwargs)[source]#

Convert any reasonable object – a string, integer, or datetime object, or list/array of any of those – to a date object (or string, pandas, or numpy date).

If the object is an integer, this is interpreted as follows:

  • With readformat=’posix’: treat as a POSIX timestamp, in seconds from 1970

  • With readformat=’ordinal’/’matplotlib’: treat as an ordinal number of days from 1970 (Matplotlib default)

  • With start_date provided: treat as a number of days from this date

Note: in this and other date functions, arguments work either with or without underscores (e.g. start_date or startdate)

Parameters:
  • obj (str/int/date/datetime/list/array) – the object to convert; if None, return current date

  • args (str/int/date/datetime) – additional objects to convert

  • start_date (str/date/datetime) – the starting date, if an integer is supplied

  • readformat (str/list) – the format to read the date in; passed to sc.readdate() (NB: can also use “format” instead of “readformat”)

  • to (str) – the output format: ‘date’ (default), ‘datetime’, ‘str’ (or ‘string’), ‘pandas’, or ‘numpy’

  • as_date (bool) – alternate method of choosing between output format of ‘date’ (True) or ‘str’ (False); if None, use “to” instead

  • outformat (str) – the format to output the date in, if returning a string

  • kwargs (dict) – only used for deprecated argument aliases

Returns:

either a single date object, or a list of them (matching input data type where possible)

Return type:

dates (date or list)

Examples:

sc.date('2020-04-05') # Returns datetime.date(2020, 4, 5)
sc.date([35,36,37], start_date='2020-01-01', to='str') # Returns ['2020-02-05', '2020-02-06', '2020-02-07']
sc.date(1923288822, readformat='posix') # Interpret as a POSIX timestamp
New in version 1.0.0.
New in version 1.2.2: “readformat” argument; renamed “dateformat” to “outformat”
New in version 2.0.0: support for np.datetime64 objects
New in version 3.0.0: added “to” argument, and support for pd.Timestamp and np.datetime64 output; allow None
New in version 3.1.0: allow “datetime” output