1
- import datetime as dt
2
- from typing import Hashable
1
+ from typing import Hashable , Union
3
2
4
3
import pandas as pd
5
4
import pandas_flavor as pf
6
- from pandas .api .types import is_numeric_dtype
7
5
from pandas .errors import OutOfBoundsDatetime
8
6
9
- from janitor .utils import deprecated_alias
7
+ from janitor .utils import deprecated_alias , refactored_function
10
8
11
9
12
10
@pf .register_dataframe_method
13
- @deprecated_alias (column = "column_name " )
11
+ @deprecated_alias (column = "column_names " )
14
12
def convert_excel_date (
15
- df : pd .DataFrame , column_name : Hashable
13
+ df : pd .DataFrame , column_names : Union [ Hashable , list ]
16
14
) -> pd .DataFrame :
17
15
"""Convert Excel's serial date format into Python datetime format.
18
16
19
- This method mutates the original DataFrame.
17
+ This method does not mutate the original DataFrame.
20
18
21
- Implementation is also from
19
+ Implementation is based on
22
20
[Stack Overflow](https://stackoverflow.com/questions/38454403/convert-excel-style-date-with-pandas).
23
21
24
22
Examples:
@@ -38,40 +36,36 @@ def convert_excel_date(
38
36
39
37
Args:
40
38
df: A pandas DataFrame.
41
- column_name: A column name.
42
-
43
- Raises:
44
- ValueError: If there are non numeric values in the column.
39
+ column_names: A column name, or a list of column names.
45
40
46
41
Returns:
47
42
A pandas DataFrame with corrected dates.
48
43
""" # noqa: E501
49
44
50
- if not is_numeric_dtype (df [column_name ]):
51
- raise ValueError (
52
- "There are non-numeric values in the column. "
53
- "All values must be numeric."
45
+ if not isinstance (column_names , list ):
46
+ column_names = [column_names ]
47
+ # https://stackoverflow.com/a/65460255/7175713
48
+ dictionary = {
49
+ column_name : pd .to_datetime (
50
+ df [column_name ], unit = "D" , origin = "1899-12-30"
54
51
)
52
+ for column_name in column_names
53
+ }
55
54
56
- df [column_name ] = pd .TimedeltaIndex (
57
- df [column_name ], unit = "d"
58
- ) + dt .datetime (
59
- 1899 , 12 , 30
60
- ) # noqa: W503
61
- return df
55
+ return df .assign (** dictionary )
62
56
63
57
64
58
@pf .register_dataframe_method
65
- @deprecated_alias (column = "column_name " )
59
+ @deprecated_alias (column = "column_names " )
66
60
def convert_matlab_date (
67
- df : pd .DataFrame , column_name : Hashable
61
+ df : pd .DataFrame , column_names : Union [ Hashable , list ]
68
62
) -> pd .DataFrame :
69
63
"""Convert Matlab's serial date number into Python datetime format.
70
64
71
- Implementation is also from
65
+ Implementation is based on
72
66
[Stack Overflow](https://stackoverflow.com/questions/13965740/converting-matlabs-datenum-format-to-python).
73
67
74
- This method mutates the original DataFrame.
68
+ This method does not mutate the original DataFrame.
75
69
76
70
Examples:
77
71
>>> import pandas as pd
@@ -84,29 +78,38 @@ def convert_matlab_date(
84
78
2 737124.498500
85
79
3 737124.000000
86
80
>>> df.convert_matlab_date('date')
87
- date
88
- 0 2018-03-06 00:00:00.000000
89
- 1 2018-03-05 19:34:50.563200
90
- 2 2018-03-05 11:57:50.399999
91
- 3 2018-03-05 00:00:00.000000
81
+ date
82
+ 0 2018-03-06 00:00:00.000000000
83
+ 1 2018-03-05 19:34:50.563199671
84
+ 2 2018-03-05 11:57:50.399998876
85
+ 3 2018-03-05 00:00:00.000000000
92
86
93
87
Args:
94
88
df: A pandas DataFrame.
95
- column_name : A column name.
89
+ column_names : A column name, or a list of column names .
96
90
97
91
Returns:
98
92
A pandas DataFrame with corrected dates.
99
93
""" # noqa: E501
100
- days = pd . Series ([ dt . timedelta ( v % 1 ) for v in df [ column_name ]])
101
- df [ column_name ] = (
102
- df [ column_name ]. astype ( int ). apply ( dt . datetime . fromordinal )
103
- + days
104
- - dt . timedelta ( days = 366 )
105
- )
106
- return df
94
+ # https://stackoverflow.com/a/49135037/7175713
95
+ if not isinstance ( column_names , list ):
96
+ column_names = [ column_names ]
97
+ dictionary = {
98
+ column_name : pd . to_datetime ( df [ column_name ] - 719529 , unit = "D" )
99
+ for column_name in column_names
100
+ }
107
101
102
+ return df .assign (** dictionary )
108
103
104
+
105
+ @pf .register_dataframe_method
109
106
@pf .register_dataframe_method
107
+ @refactored_function (
108
+ message = (
109
+ "This function will be deprecated in a 1.x release. "
110
+ "Please use `pd.to_datetime` instead."
111
+ )
112
+ )
110
113
@deprecated_alias (column = "column_name" )
111
114
def convert_unix_date (df : pd .DataFrame , column_name : Hashable ) -> pd .DataFrame :
112
115
"""Convert unix epoch time into Python datetime format.
@@ -116,6 +119,11 @@ def convert_unix_date(df: pd.DataFrame, column_name: Hashable) -> pd.DataFrame:
116
119
117
120
This method mutates the original DataFrame.
118
121
122
+ !!!note
123
+
124
+ This function will be deprecated in a 1.x release.
125
+ Please use `pd.to_datetime` instead.
126
+
119
127
Examples:
120
128
>>> import pandas as pd
121
129
>>> import janitor
0 commit comments