Tag Info

Hot answers tagged

46

The short answer is, yes! There is a whole undocumented package TemporalData` containing some useful functions. The results below are from my own spelunking. Feel free to add/amend as appropriate. Let's set up some simple TemporalData objects to explore them: fakedata = Transpose@{DatePlus[{2001, 1}, {#, "Month"}] & /@ Range[0, 99], ...


24

Just a literal implementation of a formula for the day of the week: Clear[dow]; dow[{year_, month_, day_, _ : 0, _ : 0, _ : 0}] := Module[{Y = If[month == 1 || month == 2, year - 1, year], m = Mod[month + 9, 12] + 1, y, c, s = {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}}, y = Mod[Y, 100]; c = Quotient[Y, 100]; ...


24

Date-picker implementation in Mathematica The following is my implementation of a simple date-picker. The current date is highlighted in LightBlue and the weekends are highlighted in LightGreen. The selected date is always highlighted in LightRed (the default selection is the current date). You can tap into this calendar by using the Dynamic values for ...


21

I will provide one solution which will be using Java and a simple Java reloader I recently introduced. This solution brings to the table up to 100-fold speed-up for large lists of dates. Preparation I will borrow @Mike's functions to generate a random list of dates, from his code in his recent question RandomDateList[] := { RandomInteger[{1800, 2100}], ...


18

I tried to do something similar a few months ago The easiest way is to write several functions: EventFrame function creates event lables EventFrame[str_, {date_, height_}, OptionsPattern[FontSize -> 14]] := Graphics[{ Black, Thick, Line[{{date, height}, {date, 0}}], Text[Framed[Style[str, FontSize -> OptionValue[FontSize]], {Background ...


18

This site has exactly what you want here, already in Mathematica code. One example here:


17

Since I'm living in Europe I'm sticking to the European definition of week number which is equivalent to the ISO standard. According to this standard, a week starts on Monday and the first week is the week containing 4 January. Taking this into account you could do therefore do something like weekNumberISO[date_] := Module[{day0, year}, With[{days = ...


16

You can use DateDifference to find the time between January 1st and April 17th: DateDifference["Jan. 1", "April 17", "Week"] (* {15.2857, "Week"} *) If you want the "week number" as you've put it, you can just do: Ceiling@First@DateDifference["Jan. 1", "April 17", "Week"] which gives 16. Edit based on Szabolcs's comment: To ensure this works for Jan ...


16

The format specification for DateList is pretty flexible. Since we know that we have <h3> tags wrapped around things, we can just account for them: DateList[{#, {"<h3>", "MonthName", "Day", ",", "Year", "</h3>"}}] & /@ Flatten[dates] (* ==> {{2001, 1, 18, 0, 0, 0.}, {2001, 2, 1, 0, 0, 0.}, {2001, 2, 2, 0, 0, 0.}, {2001, 2, ...


16

It took me quite a while, but finally, here's a visualization of the perigee of Flamsteed's comet: I should first note two things: first, some of the needed data for computing the orbit of comet C/1683 O1 was missing in AstronomicalData["CometC1683O1", "Properties"], and I had to pull information from external sources to supplement the information ...


16

This question might be considered a duplicate. It is closely related to these: Considerations when determining efficiency of Mathematica code Difference between AbsoluteTiming and Timing Benchmarking expressions Profiling from Mathematica However, one simple reading of this question that I do not believe is covered in the answers above is answered with ...


13

Import[(* file *), "Table", "DateStringFormat" -> {"Year", "-", "Month", "-", "Day"}] seems to work... As a test: Export["test.dat", {{"2010-05-19", 17}, {"2010-05-20", 20}, {"2010-05-21", 19}}, "FieldSeparators" -> " "]; Import["test.dat", "Table", "DateStringFormat" -> {"Year", "-", "Month", "-", "Day"}] {{{2010, 5, 19}, ...


13

Using Simon's data: In[6]:= datelist = {"29/02/2008", "15/12/2007", "06/09/2007", "06/10/2008", "05/03/2007", "24/01/2010", "19/06/2009", "03/11/2009", "02/02/2010", "25/12/2009"}; We can just sort the data by the absolute time: In[7]:= SortBy[datelist, AbsoluteTime[{#, {"Day", "Month", "Year"}}] &] Out[7]= {"05/03/2007", "06/09/2007", ...


13

For full ranges There is the function DayRange that can be used for this purpose, but not in the same simple way like CharacterRange. For the days: DayName /@ DayRange[Today, Today ~DatePlus~ {{1, "Week"}, {-1, "Day"}}] {Wednesday, Thursday, Friday, Saturday, Sunday, Monday, Tuesday} For the months: DateValue[#, "MonthName"] & /@ DayRange[Today, ...


12

I've shown off Larsen's method before (and see this as well), but here it is as a formal answer: larsen[{yr_Integer, mo_Integer, da_Integer, ___}] := Module[{y = yr, m = mo, d = da, q}, If[m < 3, y--; m += 12]; q = d + 2 m + 1 + Quotient[3 (m + 1), 5] + y + Quotient[y, 4] + Quotient[y, 400] - Quotient[y, 100]; {Sunday, Monday, Tuesday, ...


12

data = FinancialData["SPY", "Jan. 1, 2011"] /. {d_List, v_} :> {AbsoluteTime@d, v}; model = a x^4 + b x^3 + c x^2 + d x + e; fit = FindFit[data, model, {a, b, c, d, e}, x] modelf = Function[{x}, Evaluate[model /. fit]] Plot[modelf[x], {x, Min@data[[All, 1]], Max@data[[All, 1]]}, Epilog -> Map[Point, data]] Edit Better (tick labels showing dates) ...


12

Jean Meeus's Astronomical Algorithms (as well as the related book Astronomical Formulæ for Calculators) is what you should start looking at whenever you need to deal with algorithms for quantities of astronomical interest. For instance, here is a translation of Meeus's method for the Julian Date: Options[jd] = {"Calendar" -> "Gregorian"}; ...


12

A simple string-based approach is to swap the order of day/month/year, do the Sort and then swap back again: (* Example data *) datelist = DateString[# + AbsoluteTime[{2007, 01, 01}], {"Day", "/", "Month", "/", "Year"}] & /@ RandomInteger[10^8, 10] {"29/02/2008", "15/12/2007", "06/09/2007", "06/10/2008", "05/03/2007", "24/01/2010", ...


12

data = Cases[DayRange[{2000, 1, 1}, {2399, 12, 31}, Friday], {y_, m_, d_} :>d] // Tally // Sort; TableForm[data[[ ;; ;; 2]], TableHeadings -> {None, {"Day", "Number of Fri."}}] (Reverse@SortBy[data, Last])[[;; 5]] {{27, 688}, {20, 688}, {13, 688}, {6, 688}, {25, 687}}


12

This is how you can get the last 30 weekdays starting from yesterday: days = DayRange[DayPlus[Yesterday, -30], Yesterday, "Weekday"] To get {y,m,d} vectors, we might use Take[#, 3] &@*DateList /@ days Regarding the time zone, the only problem I can see would be that Yesterday will not produce the right result around midnight. To check this I used ...


12

Another, shorter way is Append[d, t] From the docs, DateObject[date,time] represents the specified date list and TimeObject time. If you need the list you mention in the question, just convert the DateObject using DateList: DateList@Append[d, t] (* {2012, 6, 11, 14, 1, 45.} *)


11

Assuming that the days and times in the gathered data list are strings you could do something like days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; data2 = Reap[ Sow[(AbsoluteTime[#2] - AbsoluteTime["00:00:00"])/3600., #1] & @@@ Flatten[data, 1], days, #2 &][[2, All, 1]]; ...


11

To fit a function and to calculate the moving average you need to convert your dates in absolute time using AbsoluteTime[]. data = FinancialData["IBM", "Jan. 1, 2004"]; newdata = Table[{AbsoluteTime[data[[i, 1]]], data[[i, 2]]}, {i, Length[data]}]; lm = LinearModelFit[newdata, x, x]; movAvg = MovingAverage[newdata, 200]; ...


11

So this generates the heatmap: << Calendar` year = 1990; yearLen = DaysBetween[{year, 1, 1}, {year, 12, 31}] + 1; data = RandomReal[1, yearLen]; days = Map[DayOfWeek[{year, 1, #}] &, Range[3, 9]]; day1 = Position[days, DayOfWeek[{year, 1, 1}]][[1, 1]]; dayn = Position[days, DayOfWeek[{year, 12, 1}]][[1, 1]]; Paddata = Join[ConstantArray[100, day1 ...


11

Here's a TL;DR answer. For more details, follow Mr. Wizard's links. Timing measures the computation time consumed by the kernel process, thus On a 4-core machine, internally parallelized functions such as LinearSolve will show 4-times the Timing, i.e. the sum of CPU time used by each core Pause doesn't use CPU time so it's not included in the Timing. ...


11

Based on data in Comm ACM. This took a while to only partially automate, largely through a helper function that spreads out the years: diffuse[a_][years_List] := Module[{x0 = 1, x1 = Length[years], y0 = Min[years], y1 = Max[years]}, years // MapIndexed[ {#1, (((y1 - y0)/(x1 - x0))*(First[#2] - x0) + y0) a + (#1) (1 - a)} ...


11

I can confirm the problem in 10.0.2 under Windows. I think it can only be a bug. One can at least work around the problem in this example using MinimalBy and MaximalBy: times2 = RandomSample[times]; (* start with a random order *) MinimalBy[times2, Identity, 1] // Timing {0., {TimeObject[{0, 14, 55.99}]}} MaximalBy[times2, Identity, 1] // Timing ...


10

I'll start with a slightly reformatted version of your data: data = {{"Monday", 3}, {"Tuesday", 4}, {"Wednesday", 6}, {"Thursday", 6}, {"Friday", 10}, {"Saturday", 12}, {"Sunday", 11}, {"Monday", 3}, {"Tuesday", 4}, {"Wednesday", 4}, {"Thursday", 5}, {"Friday", 10}, {"Saturday", 10}, {"Sunday", 9}, {"Monday", 2}, {"Tuesday", 3}, ...


10

This recent post reminded me that AbsoluteTime is a fast kernel function. Using the RandomDates function from Leonid's post: dates = RandomDates[500000]; Needs["Calendar`"] rls = Thread[ Range[0, 6] -> {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday} ]; Timing[result1 = DayOfWeek /@ dates;] Timing[result2 = ...


10

I confess to being allergic to database operations that require data to be in a particular sort order: it's too easy for huge mistakes to creep in. What is needed here is to turn the source list (say, list1) into a lookup table so it reliably returns its value (second element in the list) when given its key (first element in the list). To assure ...



Only top voted, non community-wiki answers of a minimum length are eligible