8000 API/COMPAT: add pydatetime-style positional args to Timestamp constructor by thejohnfreeman · Pull Request #12482 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

API/COMPAT: add pydatetime-style positional args to Timestamp constructor #12482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix docstring; add tests for kwargs
  • Loading branch information
thejohnfreeman committed May 13, 2016
commit 6fad30bc6150f00f69fbb49ebb913b02d7397d42
33 changes: 33 additions & 0 deletions pandas/tseries/tests/test_tslib.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,39 @@ def test_constructor_positional(self):
self.assertEqual(Timestamp(*pos_args).to_pydatetime(),
datetime.datetime(*pos_args))

def test_constructor_keyword(self):
# GH 10758
with tm.assertRaises(TypeError):
Timestamp(year=2000, month=1)
with tm.assertRaises(ValueError):
Timestamp(year=2000, month=0, day=1)
with tm.assertRaises(ValueError):
Timestamp(year=2000, month=13, day=1)
with tm.assertRaises(ValueError):
Timestamp(year=2000, month=1, day=0)
with tm.assertRaises(ValueError):
Timestamp(year=2000, month=1, day=32)

ts = Timestamp(year=2000, month=1, day=2)

actual = ts.to_pydatetime()
expected = datetime.datetime(year=2000, month=1, day=2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this also seems superfluous (the .to_pydatetime() check)

self.assertEqual(actual, expected)
self.assertEqual(type(actual), type(expected))

pos_args = [2000, 1, 2, 3, 4, 5, 999999]
kw_args = {
'year': 2000,
'month': 1,
'day': 2,
'hour': 3,
'minute': 4,
'second': 5,
'microsecond': 999999,
}
self.assertEqual(Timestamp(**kw_args).to_pydatetime(),
datetime.datetime(**kw_args))

def test_conversion(self):
# GH 9255
ts = Timestamp('2000-01-01')
Expand Down
8000 24 changes: 21 additions & 3 deletions pandas/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,12 @@ class Timestamp(_Timestamp):
for the entries that make up a DatetimeIndex, and other timeseries
oriented data structures in pandas.

Parameters
----------
There are essentially three calling conventions for the constructor. The
first, legacy form accepts four parameters. They can be passed by
position or keyword.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a versionadded tag here (0.18.2)


Legacy Parameters
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't call these Legacy Parameters; these are in fact the normal calling convention. The datetime.datetime convention are the compat parameters (but you don't have to call it differently).

-----------------
ts_input : datetime-like, str, int, float
Value to be converted to Timestamp
offset : str, DateOffset
Expand All @@ -235,6 +239,20 @@ class Timestamp(_Timestamp):
Time zone for time which Timestamp will have.
unit : string
numpy unit used for conversion, if ts_input is int or float

The other two forms copy the parameters from datetime.datetime. They can
be passed by either position or keyword, but not both mixed together.

datetime.datetime Parameters
------------------------------
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put a refernce to datetime.datetime doc page

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the format for that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    .. versionadded:: 0.18.2

be sure to put a blank line before and after (otherwise we get warnings)

year : int
month : int
day : int
hour : int [optional]
minute : int [optional]
second : int [optional]
microsecond : int [optional]
tzinfo : datetime.tzinfo [optional]
"""

@classmethod
Expand Down Expand Up @@ -319,7 +337,7 @@ class Timestamp(_Timestamp):
return Timestamp(datetime(year, month, day, hour or 0,
minute or 0, second or 0, microsecond or 0, tzinfo),
tz=tzinfo)
if is_integer_object(offset):
elif is_integer_object(offset):
# User passed positional arguments:
# Timestamp(year, month, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]])
return Timestamp(datetime(ts_input, offset, tz, unit or 0,
Expand Down
0