|
| 1 | +# |
| 2 | +# Copyright (C) 2019 Databricks, Inc. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | + |
| 17 | +from distutils.version import LooseVersion |
| 18 | + |
| 19 | +import pandas as pd |
| 20 | +import databricks.koalas as ks |
| 21 | + |
| 22 | +from databricks.koalas.testing.utils import ReusedSQLTestCase, TestUtils |
| 23 | + |
| 24 | + |
| 25 | +class DatetimeIndexTest(ReusedSQLTestCase, TestUtils): |
| 26 | + @property |
| 27 | + def fixed_freqs(self): |
| 28 | + return [ |
| 29 | + "D", |
| 30 | + "H", |
| 31 | + "T", # min |
| 32 | + "S", |
| 33 | + "L", # ms |
| 34 | + "U", # us |
| 35 | + # 'N' not supported |
| 36 | + ] |
| 37 | + |
| 38 | + @property |
| 39 | + def non_fixed_freqs(self): |
| 40 | + return ["W", "Q"] |
| 41 | + |
| 42 | + @property |
| 43 | + def pidxs(self): |
| 44 | + return [ |
| 45 | + pd.DatetimeIndex([0]), |
| 46 | + pd.DatetimeIndex(["2004-01-01", "2002-12-31", "2000-04-01"]), |
| 47 | + ] + [ |
| 48 | + pd.date_range("2000-01-01", periods=3, freq=freq) |
| 49 | + for freq in (self.fixed_freqs + self.non_fixed_freqs) |
| 50 | + ] |
| 51 | + |
| 52 | + @property |
| 53 | + def kidxs(self): |
| 54 | + return [ks.from_pandas(pidx) for pidx in self.pidxs] |
| 55 | + |
| 56 | + @property |
| 57 | + def idx_pairs(self): |
| 58 | + return list(zip(self.kidxs, self.pidxs)) |
| 59 | + |
| 60 | + def test_properties(self): |
| 61 | + for kidx, pidx in self.idx_pairs: |
| 62 | + self.assert_eq(kidx.year, pidx.year) |
| 63 | + self.assert_eq(kidx.month, pidx.month) |
| 64 | + self.assert_eq(kidx.day, pidx.day) |
| 65 | + self.assert_eq(kidx.hour, pidx.hour) |
| 66 | + self.assert_eq(kidx.minute, pidx.minute) |
| 67 | + self.assert_eq(kidx.second, pidx.second) |
| 68 | + self.assert_eq(kidx.microsecond, pidx.microsecond) |
| 69 | + self.assert_eq(kidx.week, pidx.week) |
| 70 | + self.assert_eq(kidx.weekofyear, pidx.weekofyear) |
| 71 | + self.assert_eq(kidx.dayofweek, pidx.dayofweek) |
| 72 | + self.assert_eq(kidx.weekday, pidx.weekday) |
| 73 | + self.assert_eq(kidx.dayofyear, pidx.dayofyear) |
| 74 | + self.assert_eq(kidx.quarter, pidx.quarter) |
| 75 | + self.assert_eq(kidx.daysinmonth, pidx.daysinmonth) |
| 76 | + self.assert_eq(kidx.days_in_month, pidx.days_in_month) |
| 77 | + self.assert_eq(kidx.is_month_start, pd.Index(pidx.is_month_start)) |
| 78 | + self.assert_eq(kidx.is_month_end, pd.Index(pidx.is_month_end)) |
| 79 | + self.assert_eq(kidx.is_quarter_start, pd.Index(pidx.is_quarter_start)) |
| 80 | + self.assert_eq(kidx.is_quarter_end, pd.Index(pidx.is_quarter_end)) |
| 81 | + self.assert_eq(kidx.is_year_start, pd.Index(pidx.is_year_start)) |
| 82 | + self.assert_eq(kidx.is_year_end, pd.Index(pidx.is_year_end)) |
| 83 | + self.assert_eq(kidx.is_leap_year, pd.Index(pidx.is_leap_year)) |
| 84 | + |
| 85 | + if LooseVersion(pd.__version__) >= LooseVersion("1.2.0"): |
| 86 | + self.assert_eq(kidx.day_of_year, pidx.day_of_year) |
| 87 | + self.assert_eq(kidx.day_of_week, pidx.day_of_week) |
0 commit comments