-
Notifications
You must be signed in to change notification settings - Fork 224
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
Use np.asarray to convert a 1-D array to datetime type in array_to_datetime #2481
Changes from all commits
1de890b
608e29b
2ac723e
7593354
51a4425
ada5616
05c8b93
7c3c866
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
import warnings | ||
|
||
import numpy as np | ||
import pandas as pd | ||
from pygmt.exceptions import GMTInvalidInput | ||
|
||
|
||
|
@@ -252,10 +251,9 @@ def kwargs_to_ctypes_array(argument, kwargs, dtype): | |
|
||
def array_to_datetime(array): | ||
""" | ||
Convert an 1-D datetime array from various types into pandas.DatetimeIndex | ||
(i.e., numpy.datetime64). | ||
Convert a 1-D datetime array from various types into numpy.datetime64. | ||
|
||
If the input array is not in legal datetime formats, raise a "ParseError" | ||
If the input array is not in legal datetime formats, raise a ValueError | ||
exception. | ||
|
||
Parameters | ||
|
@@ -272,58 +270,58 @@ def array_to_datetime(array): | |
|
||
Returns | ||
------- | ||
array : 1-D datetime array in pandas.DatetimeIndex (i.e., numpy.datetime64) | ||
array : 1-D datetime array in numpy.datetime64 | ||
|
||
Raises | ||
------ | ||
ValueError | ||
If the datetime string is invalid. | ||
|
||
Examples | ||
-------- | ||
>>> import datetime | ||
>>> # numpy.datetime64 array | ||
>>> x = np.array( | ||
... ["2010-06-01", "2011-06-01T12", "2012-01-01T12:34:56"], | ||
... dtype="datetime64", | ||
... dtype="datetime64[ns]", | ||
... ) | ||
>>> array_to_datetime(x) | ||
DatetimeIndex(['2010-06-01 00:00:00', '2011-06-01 12:00:00', | ||
'2012-01-01 12:34:56'], | ||
dtype='datetime64[ns]', freq=None) | ||
array(['2010-06-01T00:00:00.000000000', '2011-06-01T12:00:00.000000000', | ||
'2012-01-01T12:34:56.000000000'], dtype='datetime64[ns]') | ||
|
||
>>> # pandas.DateTimeIndex array | ||
>>> import pandas as pd | ||
>>> x = pd.date_range("2013", freq="YS", periods=3) | ||
>>> array_to_datetime(x) # doctest: +NORMALIZE_WHITESPACE | ||
DatetimeIndex(['2013-01-01', '2014-01-01', '2015-01-01'], | ||
dtype='datetime64[ns]', freq='AS-JAN') | ||
>>> array_to_datetime(x) | ||
array(['2013-01-01T00:00:00.000000000', '2014-01-01T00:00:00.000000000', | ||
'2015-01-01T00:00:00.000000000'], dtype='datetime64[ns]') | ||
|
||
>>> # Python's built-in date and datetime | ||
>>> x = [datetime.date(2018, 1, 1), datetime.datetime(2019, 1, 1)] | ||
>>> array_to_datetime(x) # doctest: +NORMALIZE_WHITESPACE | ||
DatetimeIndex(['2018-01-01', '2019-01-01'], | ||
dtype='datetime64[ns]', freq=None) | ||
>>> array_to_datetime(x) | ||
array(['2018-01-01T00:00:00.000000', '2019-01-01T00:00:00.000000'], | ||
dtype='datetime64[us]') | ||
|
||
>>> # Raw datetime strings in various format | ||
>>> x = [ | ||
... "2018", | ||
... "2018-02", | ||
... "2018-03-01", | ||
... "2018-04-01T01:02:03", | ||
... "5/1/2018", | ||
... "Jun 05, 2018", | ||
... "2018/07/02", | ||
... ] | ||
>>> array_to_datetime(x) | ||
DatetimeIndex(['2018-01-01 00:00:00', '2018-02-01 00:00:00', | ||
'2018-03-01 00:00:00', '2018-04-01 01:02:03', | ||
'2018-05-01 00:00:00', '2018-06-05 00:00:00', | ||
'2018-07-02 00:00:00'], | ||
dtype='datetime64[ns]', freq=None) | ||
array(['2018-01-01T00:00:00', '2018-02-01T00:00:00', | ||
'2018-03-01T00:00:00', '2018-04-01T01:02:03'], | ||
dtype='datetime64[s]') | ||
|
||
>>> # Mixed datetime types | ||
>>> x = [ | ||
... "2018-01-01", | ||
... np.datetime64("2018-01-01"), | ||
... datetime.datetime(2018, 1, 1), | ||
... ] | ||
>>> array_to_datetime(x) # doctest: +NORMALIZE_WHITESPACE | ||
DatetimeIndex(['2018-01-01', '2018-01-01', '2018-01-01'], | ||
dtype='datetime64[ns]', freq=None) | ||
>>> array_to_datetime(x) | ||
array(['2018-01-01T00:00:00.000000', '2018-01-01T00:00:00.000000', | ||
'2018-01-01T00:00:00.000000'], dtype='datetime64[us]') | ||
""" | ||
return pd.to_datetime(array) | ||
return np.asarray(array, dtype=np.datetime64) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like there was a reason we used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In the Plotting datetime charts tutorial, we probably need to add a paragraph or a subsection to explain that, for non-ISO datetimes like 'Jun 05, 2018', people should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Under https://www.pygmt.org/v0.9.0/tutorials/advanced/date_time_charts.html#generating-an-automatic-region, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a sentence under the "Using ISO Format" section (ada5616). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These three types of datetime inputs are no longer supported. Actually, they're never supported because they won't be recognized as valid datetime strings in the
_check_dtype_and_dim
function.