convert pandas time column to datetime column - pandas

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

Related

How to convert string to datetime as column header

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

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"

How Do I Query for the Time Portion of a Timestamp in DB2?

Right now I'm trying to check a timestamp column, and find out if the schedule I'm looking at starts at midnight. I don't want to check the date portion.
So far I've tried a few things, but they are all similar to this.
SELECT * FROM schedule_summary
WHERE sche_START_TIME = TIME('00:00:00');
time(Sche_START_TIME) = Time('00:00:00')
Convert both to time data types and compare them.
Though you need to be careful if timezones for either component could be different or if you need to account for variances in geographic location and times.

How can I store date only in datetime field in WebMatrix with Sql Server CE?

I was wondering if there was a way to store a date (example: 01/01/2013) as datetime without SQL Server CE adding the time (example: 12:00:00 AM).
I could always store it as the string "01/01/2013" but I really want to be able to compare the dates on querying the database.
I realize that as long as I only stored the date part, all of the times in the datetime field would have equal values (i.e. 12:00:00 AM), so comparing them wouldn't be a problem and I could just always ignore the time part, however, it seems ridiculous to have this unnecessary data appended to every entry in the table.
Is there a way to store only the date part of the datetime as datetime so that the dates can still be compared in the SQL query or do I just need to live with this overhead and move on?
Side Note:
I just spent the last 30 minutes searching Google and SO for an answer I was sure was already out there, but to my surprise, I couldn't find anything on this issue.
Update:
The conclusion I have come to is that I will just accept the time in the datetime format and let it always default to 12:00:00 AM by only adding the date part during the INSERT statement (e.g. 01/01/2013). As long as the time part always remains the same throughout, the dates will still be easily comparable and I can just trim it up when I convert it to string for screen display. I believe this will be the easiest way to handle this scenario. After all, I decided to use SQL for the power of its queries, otherwise, I might have just used XML instead of a database, in the first place.
No you really can't get rid of the time component. It is part of the data type defined by sql server. I was very annoyed by it until I found that I could still display the dates without the time using JQuery to reformat them with the date formatter plugi:
https://github.com/phstc/jquery-dateFormat
Good Luck!
select CONVERT(date, GETDATE())

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.