How to convert string to datetime as column header - google-colaboratory

I currently have a database that is a time series but instead of a actual time for the header of each column in just gives me Period_1, Period_2 ect.
How do I Rename all of these column headers but also convert them to datetimes in seconds starting from 0 - whatever number?
Thank you!

I'm confused why you'd want column headers as the actual dates. That doesn't seem like it is any sort of Time Series Database with which I'm familiar. Typically a timestamp (as a data type) is a column. Not a column for each distinct timestamp. That way madness lies.
For a good example of a time series database, check out QuestDB

Related

convert pandas time column to datetime column

I have read-only access to data where time is stored as time object (date is irrelevant). I need to subtract a few seconds from each row. So the simplest way I know is to use timedelta, but first, I need to convert time column to datetime column. There should be a straight-forward way to do that; apparently there is not.
Ok, finally I found a solution that was obscured by many wired workarounds. It required format argument: pd.to_datetime(measures.time,format='%H:%M:%S'), where format must be exactly the same as time is formatted.
A one-line solution if anybody will come with similar question:
time = (pd.to_datetime(measures.time,format='%H:%M:%S') - delta).dt.time
where measures is a dataset and time is column name

How to to get part of the day from date and time column

I have a column named "date_time" which has date and time stamp of some year, I want to find which part of the day does this time fall into, like morning, noon, evening and night for extracting the features as below :
if date_time.dt.hour >=5 and new_data.current_date_time.dt.hour <12 --> then it's morning
if date_time.dt.hour >=12 and new_data.current_date_time.dt.hour<17 --> then noon
if date_time.dt.hour >=17 and new_data.current_date_time.dt.hour<20 --. then evening
else night.
But I'm unable to filter as above using .dt.hour attribute of the pandas to_datetime datatype, please help me in achieving this.
You should get the datetime from timstamp, there are multiple ways to do this, you can refer this Converting between datetime and Pandas Timestamp objects.
Once you have time object like in HH, MM, SS etc. you can use your logic for getting morning, noon, evening, night.
One point here, standard is to get AM or PM or in 24HH time, if you want to say Morning or Evening you have to match time based on your condition, not searching for direct method.
Again its my opinion.
Specifically if i answer:
Step-1 Parse the timestamp into string, get object something like
(YY-MM-DD HH:MM:SS)
Step-2 Extract HH and MM from string object
Step-3 Perform your logic by casting these string into number
This is an addition to the Shubham's answer. I assume, following what he described, you are able to extract time-stamps properly.
Further, depending on your use-case, you may want to make changes to the column itself, or you could add one more column to store these values. Let me explain the process using the later. To run through an exemplar code, let me call this column as part_of_day. This can be done as
df["C"] = ""
Now, you will have to start add values to the column based on certain conditions. The general syntax for this would be.
df.loc[<mask to generate the labels to index> , <optional column(s)>] = <some value>
For your case, one of the conditions may be look like
df.loc[5 <= df.date_time.hour and df.date_time.hour <=12 , "part_of_day"] = "morning"

SQL - Inserting a time value into column?

I am using Oracle SQL and I would like to insert a time value (eg 15:45 or 15:45:00) into a column which has a data type of TIMESTAMP. I have tried the following but It gives a error about it not being a valid month.
INSERT trainTbl(Dest, trainTime)
VALUES
('Waterloo', '15:00:00');
Would appreciate if someone could put me on the right direction.
Thanks
Oracle's TIMESTAMP data type holds a complete time and date. You cannot use it to store a time only; either store a complete time and date, or use a different data type for your column.
Some options for ways to store the time only are discussed in How to store only time; not date and time?. However, if you're already storing the date in another column, you should probably just store this information together. There is a reason Oracle provides the data types that it does.

Is it possible to add just Date in a datetime column and not get appended 00:00.000

MY Table consists of a datetime column and I wanted to know if its possible to add just Date in the column without getting appended zeros in Time part. Currently I used
CONVERT(date, getdate()) and tried to insert , but it comes in as
2013-01-20 00:00:00.000 in the column.
Pardon me if this is a very basic question
Yes, it is. Use the DATE data type instead of DATETIME.
However do not concern yourself with whether the 0s are there are not - that's not how SQL Server stores it internally, it's just how Management Studio is presenting it to you.
If you are using SQL Server you could use the Date type rather than DateTime. Otherwise the zeros will be there for the time portion. Of course you can choose not to display the time portion in your application if desired.

Sorting a time value in a dataset

In my current solution, I am converting a DateTime value, "Time" in my database using CONVERT so it displays in a ##:##AM/PM format, and I realize that CONVERT just takes whatever datatype and turns it into a VarChar, or a String once its in C#. This is making my sort work incorrectly in my Gridview. I am sorting the columns in my DataSet returned from my stored procedure. However it isnt sorting by AM/PM since its a string literal, and not a DateTime.
What is the best way to sort Time values? Should I use a different datatype, like TIME in my database? Different CONVERT command? I'm stumped! I can't use a 24 hour format, that's the only restriction. Thanks!
A couple of thoughts:
Could you use AM 01:23 / PM 01:23 format - this would sort well.
If you can return the times from the database in DATETIME format, but with the date set to some 'constant date' (e.g. 2000-01-01 hh:mm), you could almost certainly put a format string on the GridView column to display just the time, whilst still enabling sorting by the underlying value.
I use ticks: http://www.codeproject.com/KB/database/DateTimeToTicks.aspx
Or (if possible) DateTime - and handle it in my grid.
You can add an additional column with the date/converted date which is hidden in you gui but used to sort.