date#
- class 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). To convert an integer to a date, you must supply a start date.
Caution: while this function and
sc.readdate()
are similar, and indeed this function callssc.readdate()
if the input is a string, in this function an integer is treated as a number of days from start_date, while forsc.readdate()
it is treated as a timestamp in seconds.Note: in this and other date functions, arguments work either with or without underscores (e.g.
start_date
orstartdate
)- 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), ‘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:
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 fornp.datetime64
objectsNew in version 3.0.0: added “to” argument, and support forpd.Timestamp
andnp.datetime64
output; allow None