Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

1661e8f5-5b95-440f-b75e-55f1822fd7c8 2.3 KB
Raw

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  1. from pandas import (
  2. TimedeltaIndex,
  3. timedelta_range,
  4. )
  5. import pandas._testing as tm
  6. class TestTimedeltaIndexDelete:
  7. def test_delete(self):
  8. idx = timedelta_range(start="1 Days", periods=5, freq="D", name="idx")
  9. # preserve freq
  10. expected_0 = timedelta_range(start="2 Days", periods=4, freq="D", name="idx")
  11. expected_4 = timedelta_range(start="1 Days", periods=4, freq="D", name="idx")
  12. # reset freq to None
  13. expected_1 = TimedeltaIndex(
  14. ["1 day", "3 day", "4 day", "5 day"], freq=None, name="idx"
  15. )
  16. cases = {
  17. 0: expected_0,
  18. -5: expected_0,
  19. -1: expected_4,
  20. 4: expected_4,
  21. 1: expected_1,
  22. }
  23. for n, expected in cases.items():
  24. result = idx.delete(n)
  25. tm.assert_index_equal(result, expected)
  26. assert result.name == expected.name
  27. assert result.freq == expected.freq
  28. with tm.external_error_raised((IndexError, ValueError)):
  29. # either depending on numpy version
  30. idx.delete(5)
  31. def test_delete_slice(self):
  32. idx = timedelta_range(start="1 days", periods=10, freq="D", name="idx")
  33. # preserve freq
  34. expected_0_2 = timedelta_range(start="4 days", periods=7, freq="D", name="idx")
  35. expected_7_9 = timedelta_range(start="1 days", periods=7, freq="D", name="idx")
  36. # reset freq to None
  37. expected_3_5 = TimedeltaIndex(
  38. ["1 d", "2 d", "3 d", "7 d", "8 d", "9 d", "10d"], freq=None, name="idx"
  39. )
  40. cases = {
  41. (0, 1, 2): expected_0_2,
  42. (7, 8, 9): expected_7_9,
  43. (3, 4, 5): expected_3_5,
  44. }
  45. for n, expected in cases.items():
  46. result = idx.delete(n)
  47. tm.assert_index_equal(result, expected)
  48. assert result.name == expected.name
  49. assert result.freq == expected.freq
  50. result = idx.delete(slice(n[0], n[-1] + 1))
  51. tm.assert_index_equal(result, expected)
  52. assert result.name == expected.name
  53. assert result.freq == expected.freq
  54. def test_delete_doesnt_infer_freq(self):
  55. # GH#30655 behavior matches DatetimeIndex
  56. tdi = TimedeltaIndex(["1 Day", "2 Days", None, "3 Days", "4 Days"])
  57. result = tdi.delete(2)
  58. assert result.freq is None
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...