Skip to content

Commit 9ac8e1a

Browse files
committed
DEPR: convert_datetime64 parameter in to_records()
1 parent 670c2e4 commit 9ac8e1a

File tree

7 files changed

+184
-3
lines changed

7 files changed

+184
-3
lines changed

doc/source/whatsnew/v0.23.0.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,7 @@ Deprecations
740740

741741
- ``pandas.tseries.plotting.tsplot`` is deprecated. Use :func:`Series.plot` instead (:issue:`18627`)
742742
- ``Index.summary()`` is deprecated and will be removed in a future version (:issue:`18217`)
743+
- The ``convert_datetime64`` parameter has been deprecated and the default value is now ``False`` in :func:`to_records` as the NumPy bug motivating this parameter has been resolved (:issue:`18160`).
743744

744745
.. _whatsnew_0230.prior_deprecations:
745746

hack.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import pandas as pd
2+
import pdb
3+
4+
p = pd.Period('2017-01-01', 'D')
5+
pi = pd.PeriodIndex([p])
6+
7+
#print(pd.Series(p).dt.end_time[0])
8+
#print(pd.Series(pi).dt.end_time[0])
9+
#
10+
#print(p.end_time)
11+
#print(pi.end_time[0])
12+
13+
p = pd.Period('2017-01-01', 'D')
14+
pi = pd.PeriodIndex([p])
15+
16+
print(pd.Series(p).dt.end_time[0])
17+
print(pd.Series(pi).dt.end_time[0])
18+
print(p.end_time) # x
19+
print(pi.end_time[0])
20+
print(p.to_timestamp(how='end')) # x
21+
print(pi.to_timestamp(how='end')[0])
22+
23+
"""
24+
Indicator for whether the date is the last day of the month.
25+
26+
Returns
27+
-------
28+
Series or array
29+
For Series, returns a Series with boolean values. For
30+
DatetimeIndex, returns a boolean array.
31+
32+
See Also
33+
--------
34+
is_month_start : Indicator for whether the date is the first day
35+
of the month.
36+
37+
Examples
38+
--------
39+
This method is available on Series with datetime values under
40+
the ``.dt`` accessor, and directly on DatetimeIndex.
41+
42+
>>> dates = pd.Series(pd.date_range("2018-02-27", periods=3))
43+
>>> dates
44+
0 2018-02-27
45+
1 2018-02-28
46+
2 2018-03-01
47+
dtype: datetime64[ns]
48+
>>> dates.dt.is_month_end
49+
0 False
50+
1 True
51+
2 False
52+
dtype: bool
53+
54+
>>> idx = pd.date_range("2018-02-27", periods=3)
55+
>>> idx.is_month_end
56+
array([False, True, False], dtype=bool)
57+
""")
58+
59+
"""
60+
Boolean indicator if the date belongs to a leap year.
61+
62+
A leap year is a year, which has 366 days (instead of 365) including
63+
29th of February as an intercalary day.
64+
Leap years are years which are multiples of four with the exception
65+
of years divisible by 100 but not by 400.
66+
67+
Returns
68+
-------
69+
Series or ndarray
70+
Booleans indicating if dates belong to a leap year.
71+
72+
Examples
73+
--------
74+
This method is available on Series with datetime values under
75+
the ``.dt`` accessor, and directly on DatetimeIndex.
76+
77+
>>> idx = pd.date_range("2012-01-01", "2015-01-01", freq="Y")
78+
>>> idx
79+
DatetimeIndex(['2012-12-31', '2013-12-31', '2014-12-31'],
80+
dtype='datetime64[ns]', freq='A-DEC')
81+
>>> idx.is_leap_year
82+
array([ True, False, False], dtype=bool)
83+
84+
>>> dates = pd.Series(idx)
85+
>>> dates_series
86+
0 2012-12-31
87+
1 2013-12-31
88+
2 2014-12-31
89+
dtype: datetime64[ns]
90+
>>> dates_series.dt.is_leap_year
91+
0 True
92+
1 False
93+
2 False
94+
dtype: bool
95+
""")

hacking_asfreq.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pandas as pd
2+
import pdb
3+
4+
ts = pd.Series([1.1, 1.2, 1.3, 1.4, 1.5],
5+
index=pd.period_range('1/1/1990', freq='W-MON', periods=5))
6+
7+
result = ts.resample('B', convention='end').ffill()
8+
print(result)
9+
10+
#pdb.set_trace()
11+
expected = result.to_timestamp('B', how='end').asfreq('B', 'ffill').to_period()
12+
print(expected)

hacking_offsetting.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import pandas as pd
2+
3+
vec = pd.DatetimeIndex([pd.Timestamp('2000-01-05 00:15:00'),
4+
pd.Timestamp('2000-01-31 00:23:00'),
5+
pd.Timestamp('2000-01-01 00:00:00'),
6+
pd.Timestamp('2000-03-31 00:00:00'),
7+
pd.Timestamp('2000-02-29 00:00:00'),
8+
pd.Timestamp('2000-12-31 00:00:00'),
9+
pd.Timestamp('2000-05-15 00:00:00'),
10+
pd.Timestamp('2001-06-15 00:00:00')])
11+
12+
print(isinstance(pd.tseries.offsets.Week, pd.DateOffset))
13+
14+
print(vec + pd.tseries.offsets.Week(weekday=3))
15+
16+
print('wanted')
17+
print([pd.Timestamp('2000-01-06 00:15:00'),
18+
pd.Timestamp('2000-02-03 00:23:00'),
19+
pd.Timestamp('2000-01-06 00:00:00'),
20+
pd.Timestamp('2000-04-06 00:00:00'),
21+
pd.Timestamp('2000-03-02 00:00:00'),
22+
pd.Timestamp('2001-01-04 00:00:00'),
23+
pd.Timestamp('2000-05-18 00:00:00'),
24+
pd.Timestamp('2001-06-21 00:00:00')])

hacking_resample.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import pandas as pd
2+
import numpy as np
3+
from pandas.util.testing import assert_frame_equal, assert_series_equal
4+
5+
exp_index = pd.date_range('1/1/2001', end='12/31/2009', freq='A-DEC')
6+
7+
df = pd.DataFrame(index=pd.date_range(
8+
start='2016-02-11 00:00:00',
9+
end='2016-02-12 23:00:00',
10+
freq='H'))
11+
12+
#print(df.resample('BH').count())
13+
14+
start = '1/1/1990'
15+
end = '12/31/1995'
16+
freq = 'W-FRI'
17+
rng = pd.period_range(start, end, freq=freq)
18+
ts = pd.Series(np.random.randn(len(rng)), index=rng)
19+
print(ts)
20+
result = ts.resample('B', convention='end').ffill()
21+
print(result.index)
22+
expected = result.to_timestamp('B', how='end')
23+
print(expected.index)
24+
# import pdb
25+
# pdb.set_trace()
26+
expected = expected.asfreq('B', 'ffill').to_period()
27+
28+
print('wanted')
29+
print(result.head())
30+
print(result.tail())
31+
print('received')
32+
print(expected.head())
33+
print(expected.tail())
34+
35+
print(all(result == expected))
36+
37+
assert_series_equal(result, expected)

pandas/core/frame.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,7 +1287,7 @@ def from_records(cls, data, index=None, exclude=None, columns=None,
12871287

12881288
return cls(mgr)
12891289

1290-
def to_records(self, index=True, convert_datetime64=True):
1290+
def to_records(self, index=True, convert_datetime64=False):
12911291
"""
12921292
Convert DataFrame to a NumPy record array.
12931293
@@ -1298,7 +1298,9 @@ def to_records(self, index=True, convert_datetime64=True):
12981298
----------
12991299
index : boolean, default True
13001300
Include index in resulting record array, stored in 'index' field.
1301-
convert_datetime64 : boolean, default True
1301+
convert_datetime64 : boolean, default False
1302+
.. deprecated:: 0.23.0
1303+
13021304
Whether to convert the index to datetime.datetime if it is a
13031305
DatetimeIndex.
13041306
@@ -1352,6 +1354,13 @@ def to_records(self, index=True, convert_datetime64=True):
13521354
('2018-01-01T09:01:00.000000000', 2, 0.75)],
13531355
dtype=[('index', '<M8[ns]'), ('A', '<i8'), ('B', '<f8')])
13541356
"""
1357+
1358+
if convert_datetime64:
1359+
warnings.warn("The 'convert_datetime64' parameter is "
1360+
"deprecated and will be removed in a future "
1361+
"version",
1362+
FutureWarning, stacklevel=2)
1363+
13551364
if index:
13561365
if is_datetime64_any_dtype(self.index) and convert_datetime64:
13571366
ix_vals = [self.index.to_pydatetime()]

pandas/tests/frame/test_convert_to.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ def test_to_records_dt64(self):
7878
df = DataFrame([["one", "two", "three"],
7979
["four", "five", "six"]],
8080
index=date_range("2012-01-01", "2012-01-02"))
81-
assert df.to_records()['index'][0] == df.index[0]
81+
with tm.assert_produces_warning(FutureWarning):
82+
expected = df.index[0]
83+
result = df.to_records(convert_datetime64=True)['index'][0]
84+
assert expected == result
8285

8386
rs = df.to_records(convert_datetime64=False)
8487
assert rs['index'][0] == df.index.values[0]

0 commit comments

Comments
 (0)