1
+ import Base: copy, deepcopy
2
+
1
3
function Base. copy (y:: TimeArray )
2
- return TimeArray (copy (timestamp (y)),copy (values (y)))
4
+ return TimeArray (copy (timestamp (y)), copy (values (y)))
3
5
end
4
6
5
7
function Base. deepcopy (y:: TimeArray )
6
- return TimeArray (deepcopy (timestamp (y)),deepcopy (values (y)))
8
+ return TimeArray (deepcopy (timestamp (y)), deepcopy (values (y)))
7
9
end
8
10
9
11
"""
@@ -22,11 +24,11 @@ An array of DateTime objects.
22
24
23
25
"""
24
26
function buildDatetimes (
25
- startDatetime:: T ,
26
- granularity:: P where P <: Dates.Period ,
27
- weekDaysOnly:: Bool ,
27
+ startDatetime:: T ,
28
+ granularity:: P where {P <: Dates.Period } ,
29
+ weekDaysOnly:: Bool ,
28
30
datetimesLength:: Int ,
29
- ) where T
31
+ ) where {T}
30
32
if datetimesLength == 0
31
33
return DateTime[]
32
34
end
@@ -37,7 +39,7 @@ function buildDatetimes(
37
39
currentDatetime = startDatetime
38
40
39
41
# Loop to generate timestamps based on granularity
40
- for _ in 1 : datetimesLength
42
+ for _ = 1 : datetimesLength
41
43
if weekDaysOnly && dayofweek (currentDatetime) == 5
42
44
currentDatetime += Dates. Day (3 )
43
45
else
@@ -69,15 +71,15 @@ A tuple `(granularity, frequency, weekdays)` where:
69
71
Throws an error if the timestamps do not follow a consistent pattern.
70
72
71
73
"""
72
- function identifyGranularity (datetimes:: Vector{T} ) where T
74
+ function identifyGranularity (datetimes:: Vector{T} ) where {T}
73
75
# Define base units and periods
74
76
baseUnits = [Second (1 ), Minute (1 ), Hour (1 ), Day (1 ), Week (1 )]
75
77
basePeriods = [:Second , :Minute , :Hour , :Day , :Week , :Month , :Year ]
76
-
78
+
77
79
unitPeriod = nothing
78
80
diffBetweenTimestamps = nothing
79
81
weekDaysSeries = false
80
-
82
+
81
83
for (i, unit) in enumerate (baseUnits)
82
84
differences = diff (datetimes) ./ unit
83
85
min_difference = minimum (differences)
@@ -86,13 +88,13 @@ function identifyGranularity(datetimes::Vector{T}) where T
86
88
if lessThanOne
87
89
break
88
90
end
89
-
91
+
90
92
# Check if all elements are equal
91
93
regularDistribution = all (differences .== differences[1 ])
92
94
if regularDistribution
93
95
unitPeriod = basePeriods[i]
94
96
diffBetweenTimestamps = differences[1 ]
95
-
97
+
96
98
if unit in [Minute (1 ), Second (1 )]
97
99
if diffBetweenTimestamps < 60
98
100
break
@@ -128,7 +130,7 @@ function identifyGranularity(datetimes::Vector{T}) where T
128
130
end
129
131
end
130
132
end
131
-
133
+
132
134
amplitude = maximum (differences) - min_difference
133
135
if amplitude < 1
134
136
unitPeriod = basePeriods[i]
@@ -152,10 +154,14 @@ function identifyGranularity(datetimes::Vector{T}) where T
152
154
elseif diffBetweenTimestamps % 4 == 0
153
155
unitPeriod = :Month
154
156
diffBetweenTimestamps = diffBetweenTimestamps / 4
155
- end
157
+ end
156
158
end
157
-
158
- return (granularity= unitPeriod, frequency= diffBetweenTimestamps, weekdays= weekDaysSeries)
159
+
160
+ return (
161
+ granularity = unitPeriod,
162
+ frequency = diffBetweenTimestamps,
163
+ weekdays = weekDaysSeries,
164
+ )
159
165
end
160
166
161
167
"""
@@ -171,7 +177,7 @@ Merge multiple `TimeArray` objects into a single `TimeArray`. The function align
171
177
A `TimeArray` object representing the merged time series.
172
178
173
179
"""
174
- function merge (timeArrayVector:: Vector{TimeArray} ,modelFl:: DataType = Float64)
180
+ function merge (timeArrayVector:: Vector{TimeArray} , modelFl:: DataType = Float64)
175
181
if length (timeArrayVector) == 0
176
182
return TimeArray (DateTime[], [])
177
183
end
@@ -192,19 +198,18 @@ function merge(timeArrayVector::Vector{TimeArray},modelFl::DataType=Float64)
192
198
193
199
newTimeArrayVector = []
194
200
for ta in timeArrayVector
195
- newTimeArray = from (ta,initialTimestamp)
196
- newTimeArray = to (newTimeArray,finalTimestamp)
197
- push! (newTimeArrayVector,newTimeArray)
201
+ newTimeArray = from (ta, initialTimestamp)
202
+ newTimeArray = to (newTimeArray, finalTimestamp)
203
+ push! (newTimeArrayVector, newTimeArray)
198
204
end
199
205
200
- auxiliarDf = DataFrame ((:timestamp => timestamp (newTimeArrayVector[1 ])))
206
+ auxiliarDf = DataFrame ((:timestamp => timestamp (newTimeArrayVector[1 ])))
201
207
for ta in newTimeArrayVector
202
208
# Add a column with ta colname and values
203
209
colname = colnames (ta)[1 ]
204
210
valuesTa:: Vector{modelFl} = values (ta)
205
- auxiliarDf[! ,colname] = valuesTa
211
+ auxiliarDf[! , colname] = valuesTa
206
212
end
207
213
208
- return TimeArray (auxiliarDf, timestamp= :timestamp )
214
+ return TimeArray (auxiliarDf, timestamp = :timestamp )
209
215
end
210
-
0 commit comments