Time Series Prediction and Feature Representation
Gaps in time show up as gaps in the integer values. A model that receives these numbers can detect that one observation came before another and by how much, without any date-specific logic.
Why a Model Cannot Read a Calendar Date
A time series prediction model needs numbers. A column of datetime values, such as calendar dates with clock times, cannot be fed to a model directly. The question this article answers is how a column of datetime values can become a numeric feature, and what that conversion does to the information the model actually sees.
The running example pairs a time index with a count of bike rentals recorded at each time. The rental count is the target: the value the model should learn to predict. The time index is the input that needs to be represented as a feature. These two roles must stay separate. The target stays exactly as it is, and only the time index is transformed.
What do you think happens?
Before reading further: if the time index is transformed into a feature, which part of the data should change, the rental counts (the target) or the time index (the input)?
Reveal answer
Only the time index changes. The target stays as it is.
The target is the value the model learns to predict, so it is kept separate. The conversion only applies to the datetime index, which is the input the model needs as numbers.
POSIX Time as a Count of Seconds
The conversion relies on POSIX time, a way of storing dates that is common on computers. POSIX time is a count of seconds. The count starts at January 1, 1970, at 00:00:00, which is also the starting point of Unix time. Every moment after that origin receives a single whole number equal to how many seconds have passed since then.
POSIX time stores a date as the number of seconds elapsed since January 1, 1970, at 00:00:00, which turns a timestamp into one ordered integer.
The timeline makes the counting idea concrete. The origin sits at zero, and each following day adds 86400 seconds, because a day has 24 hours of 3600 seconds each: $24 \times 3600 = 86400$. Since the spacing between consecutive days stays constant, equal gaps in time become equal gaps in the feature.
Gaps in time show up as gaps in the integer values. A model that receives these numbers can detect that one observation came before another and by how much, without any date-specific logic.
Because each later timestamp receives a larger integer, the representation preserves the order of the data. The value is a plain integer, so a model can work with it using ordinary arithmetic. That is exactly what the conversion in this article is designed to allow.
Tracing the Three-Stage Conversion
The conversion takes place in two lines of code. The first line separates the target values from the datetime index. The second line turns each timestamp in the index into an integer and arranges those integers as a column. The single expression that builds the feature hides three separate transformations: strftime, astype, and reshape. They run in this order.
- index: the datetime index holding the timestamps
strftime("%s"): formats each timestamp as text (a string representation)astype("int"): converts that text into integersreshape(-1, 1): arranges the integers as a single column
The pipeline shows the order in which the data changes shape and type. Because the three steps are chained, the final output alone does not reveal which stage went wrong. The method for locating a fault is to inspect the output of each stage in order, starting from the datetime index and moving forward until the value or shape stops matching what you expected.
From a Text Format to an Integer Column
The strftime step produces text: a formatted string representation of each timestamp. The astype step then changes the type of those values to integers. The text and the integer are different things, even though they describe the same moment. The integer is the underlying number that the feature needs, and astype is what makes that change.
index.strftime("%s")Each timestamp is held as formatted text, so the values are not yet numbers a model can do arithmetic with.
index.strftime("%s").astype("int")Each timestamp is held as an integer count of seconds, ordered so that later times have larger values.
Feature Matrix Shape After Reshape
After the integers are produced, they form a one-dimensional sequence with one entry per timestamp. The reshape(-1, 1) call rearranges that sequence into a two-dimensional column. The first dimension counts the rows, one per observation. The second dimension has size 1, because the feature matrix holds exactly one feature. The result is a feature matrix of shape $(n, 1)$, where $n$ is the number of timestamps.
Shape of X for five timestamps
Suppose the datetime index holds 5 timestamps, one per observation. What is the shape of X after the conversion?
- 1Count the observations
The index holds 5 timestamps, so n = 5 rows.
- 2Identify the feature count
There is one feature, the integer time column, so the second dimension is 1.
- 3Apply the reshape pattern
The reshape produces a matrix of shape (n, 1), which is (5, 1).
X has shape (5, 1): five rows and one column.
Mistakes Beginners Make with the Conversion
Common mistake Treating the strftime output as a number
What it looks likeUsing the result of strftime("%s") directly for arithmetic, as if it were an integer.
Why it failsstrftime produces formatted text, not an integer. The type change happens in astype.
Do this insteadApply astype("int") before relying on the values as numbers.
Common mistake Converting the target along with the time index
Why it failsThe target is the value the model should predict. Transforming it changes what the model is learning.
Do this insteadSeparate y from the datetime index first, then convert only the index.
Common mistake Judging the final result without checking each stage
Why it failsThe three chained steps hide where a wrong value or shape first appeared.
Do this insteadPrint or inspect the output after strftime, after astype, and after reshape, and stop at the first stage that does not match expectations.
Common mistake Expecting a one-dimensional result from reshape(-1, 1)
Why it failsThe reshape call exists to produce a two-dimensional matrix with one column.
Do this insteadExpect shape (n, 1), where n is the number of timestamps.
Predicting and Checking the Feature Matrix
A datetime index holds 12 hourly timestamps for bike rentals. Before running any code, predict the shape of X after the conversion, then check your answer by inspecting the output after the reshape stage.
Hints
- Count the timestamps first. Each one becomes one row.
reshape(-1, 1)gives the matrix exactly one column because there is one feature.
A conversion returns the right number of rows, but the values look like text strings such as dates formatted as characters. Which stage of the pipeline has the problem, and what would you check first?
Hints
- Compare the output of strftime with the output of astype.
- Work through the stages in order, starting from the datetime index.
Key Takeaways
- POSIX time stores a timestamp as the whole number of seconds elapsed since 1970-01-01 00:00:00, so each later moment receives a larger integer.
- The conversion chains
strftime("%s"),astype("int"), andreshape(-1, 1), turning a datetime index into a single-column feature matrix of shape (n, 1). - Gaps in time appear as gaps in the integer values, so a model can use ordinary arithmetic to detect order and spacing.
- Only the time index is transformed. The target y stays separate and unchanged.
- When a result is wrong, inspect the output after each stage in order to find the first step where it goes off track.