How to plot date times over a day - MatplotlibHow to return only the Date from a SQL Server DateTime datatypeHow do you change the size of figures drawn with matplotlib?How to get the current time in PythonHow to subtract a day from a date?How can I make a time delay in Python?Add days to JavaScript DateSave plot to image file instead of displaying it using Matplotlib“Large data” work flows using pandasHow to iterate over rows in a DataFrame in Pandas?How to make IPython notebook matplotlib plot inline

Can you turn a recording upside-down?

Are double contractions formal? Eg: "couldn't've" for "could not have"

Identity of a supposed anonymous referee revealed through "Description" of the report

What does the "DS" in "DS-..." US visa application forms stand for?

How likely are Coriolis-effect-based quirks to develop in starship crew members?

Was there a contingency plan in place if Little Boy failed to detonate?

How to get MAX value using SOQL when there are more than 50,000 rows

Is there an idiom that means "revealing a secret unintentionally"?

Renting a house to a graduate student in my department

Integral with DiracDelta. Can Mathematica be made to solve this?

Why does the electron wavefunction not collapse within atoms at room temperature in gas, liquids or solids due to decoherence?

Lorentz invariance of Maxwell's equations in matter

Row vectors and column vectors (Mathematica vs Matlab)

How do I minimise waste on a flight?

Did the IBM System/4 Pi computer have radiation-hardened versions for Skylab and Shuttle?

How can it be that ssh somename works, while nslookup somename does not?

Are there vaccine ingredients which may not be disclosed ("hidden", "trade secret", or similar)?

Compactness in normed vector spaces.

if i accidentally leaked my schools ip address and someone d doses my school am i at fault

Can the president of the United States be guilty of insider trading?

Is it safe to keep the GPU on 100% utilization for a very long time?

Is it a Munchausen Number?

Generating 10-character passwords, with 3-6 digits and 3-6 uppercase letters, in C++

Ugin's Conjurant vs. un-preventable damage



How to plot date times over a day - Matplotlib


How to return only the Date from a SQL Server DateTime datatypeHow do you change the size of figures drawn with matplotlib?How to get the current time in PythonHow to subtract a day from a date?How can I make a time delay in Python?Add days to JavaScript DateSave plot to image file instead of displaying it using Matplotlib“Large data” work flows using pandasHow to iterate over rows in a DataFrame in Pandas?How to make IPython notebook matplotlib plot inline






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I am trying to import and plot date times that go past midnight. The input dataset did not have date times attached so I had to reformat the column so that timestamps after midnight carried over to the day. This is drawing an error.



import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as dates

d = (
'Time' : ['0/1/1900 8:10:00', '0/1/1900 12:10:00', '0/1/1900 22:10:00', '1/1/1900 1:10:00'],
'Value' : [1,2,3,4],
)

df = pd.DataFrame(data = d)

df['Time'] = pd.to_datetime(df['Time'], format='%d/%m/%Y %H:%M:%S')
df.plot(x='Time', y='Value')
formatter = dates.DateFormatter('%Y-%m-%d %H:%M:%S')
plt.gcf().axes[0].xaxis.set_major_formatter(formatter)


Error:



ValueError: time data '0/1/1900 8:10:00' does not match format '%d/%m/%Y %H:%M:%S' (match)


I understand these don't match. But I'm not sure what would be the most efficient workaround. Could I add a day? Or is there a better way to just input timestamps and add 24 hours to timestamps after midnight?










share|improve this question






















  • if that is your real data the issue seems to be the zeroth January.

    – erkandem
    Mar 23 at 9:13











  • @erkandem, I understand this doesn't match. I had to create the date times via formatting. This is the output I get.

    – jonboy
    Mar 23 at 9:40

















0















I am trying to import and plot date times that go past midnight. The input dataset did not have date times attached so I had to reformat the column so that timestamps after midnight carried over to the day. This is drawing an error.



import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as dates

d = (
'Time' : ['0/1/1900 8:10:00', '0/1/1900 12:10:00', '0/1/1900 22:10:00', '1/1/1900 1:10:00'],
'Value' : [1,2,3,4],
)

df = pd.DataFrame(data = d)

df['Time'] = pd.to_datetime(df['Time'], format='%d/%m/%Y %H:%M:%S')
df.plot(x='Time', y='Value')
formatter = dates.DateFormatter('%Y-%m-%d %H:%M:%S')
plt.gcf().axes[0].xaxis.set_major_formatter(formatter)


Error:



ValueError: time data '0/1/1900 8:10:00' does not match format '%d/%m/%Y %H:%M:%S' (match)


I understand these don't match. But I'm not sure what would be the most efficient workaround. Could I add a day? Or is there a better way to just input timestamps and add 24 hours to timestamps after midnight?










share|improve this question






















  • if that is your real data the issue seems to be the zeroth January.

    – erkandem
    Mar 23 at 9:13











  • @erkandem, I understand this doesn't match. I had to create the date times via formatting. This is the output I get.

    – jonboy
    Mar 23 at 9:40













0












0








0








I am trying to import and plot date times that go past midnight. The input dataset did not have date times attached so I had to reformat the column so that timestamps after midnight carried over to the day. This is drawing an error.



import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as dates

d = (
'Time' : ['0/1/1900 8:10:00', '0/1/1900 12:10:00', '0/1/1900 22:10:00', '1/1/1900 1:10:00'],
'Value' : [1,2,3,4],
)

df = pd.DataFrame(data = d)

df['Time'] = pd.to_datetime(df['Time'], format='%d/%m/%Y %H:%M:%S')
df.plot(x='Time', y='Value')
formatter = dates.DateFormatter('%Y-%m-%d %H:%M:%S')
plt.gcf().axes[0].xaxis.set_major_formatter(formatter)


Error:



ValueError: time data '0/1/1900 8:10:00' does not match format '%d/%m/%Y %H:%M:%S' (match)


I understand these don't match. But I'm not sure what would be the most efficient workaround. Could I add a day? Or is there a better way to just input timestamps and add 24 hours to timestamps after midnight?










share|improve this question














I am trying to import and plot date times that go past midnight. The input dataset did not have date times attached so I had to reformat the column so that timestamps after midnight carried over to the day. This is drawing an error.



import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as dates

d = (
'Time' : ['0/1/1900 8:10:00', '0/1/1900 12:10:00', '0/1/1900 22:10:00', '1/1/1900 1:10:00'],
'Value' : [1,2,3,4],
)

df = pd.DataFrame(data = d)

df['Time'] = pd.to_datetime(df['Time'], format='%d/%m/%Y %H:%M:%S')
df.plot(x='Time', y='Value')
formatter = dates.DateFormatter('%Y-%m-%d %H:%M:%S')
plt.gcf().axes[0].xaxis.set_major_formatter(formatter)


Error:



ValueError: time data '0/1/1900 8:10:00' does not match format '%d/%m/%Y %H:%M:%S' (match)


I understand these don't match. But I'm not sure what would be the most efficient workaround. Could I add a day? Or is there a better way to just input timestamps and add 24 hours to timestamps after midnight?







python pandas datetime matplotlib plot






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 23 at 8:55









jonboyjonboy

175115




175115












  • if that is your real data the issue seems to be the zeroth January.

    – erkandem
    Mar 23 at 9:13











  • @erkandem, I understand this doesn't match. I had to create the date times via formatting. This is the output I get.

    – jonboy
    Mar 23 at 9:40

















  • if that is your real data the issue seems to be the zeroth January.

    – erkandem
    Mar 23 at 9:13











  • @erkandem, I understand this doesn't match. I had to create the date times via formatting. This is the output I get.

    – jonboy
    Mar 23 at 9:40
















if that is your real data the issue seems to be the zeroth January.

– erkandem
Mar 23 at 9:13





if that is your real data the issue seems to be the zeroth January.

– erkandem
Mar 23 at 9:13













@erkandem, I understand this doesn't match. I had to create the date times via formatting. This is the output I get.

– jonboy
Mar 23 at 9:40





@erkandem, I understand this doesn't match. I had to create the date times via formatting. This is the output I get.

– jonboy
Mar 23 at 9:40












1 Answer
1






active

oldest

votes


















1














The problem is that "0/1/1900" is not a correctly formatted date ("1/1/1900" would be fine). We can increment the day of the month by one:



import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as dates

d = (
'Time' : ['0/1/1900 8:10:00', '0/1/1900 12:10:00', '0/1/1900 22:10:00', '1/1/1900 1:10:00'],
'Value' : [1,2,3,4],
)
df = pd.DataFrame(data = d)

df['Time'] = ['/'.join([str(int(x.split('/')[0])+1)] + x.split('/')[1:]) for x in d['Time']]

df['Time'] = pd.to_datetime(df['Time'], format='%d/%m/%Y %H:%M:%S')
df.plot(x='Time', y='Value')
formatter = dates.DateFormatter('%Y-%m-%d %H:%M:%S')
plt.gcf().axes[0].xaxis.set_major_formatter(formatter)


Output:



enter image description here






share|improve this answer

























  • Thanks @perl. But I'm hoping to use the date time to plot values past midnight last. So the value value should be at 8:00:00 and the last one should be at 01:00:00

    – jonboy
    Mar 23 at 9:42











  • OK, I see what you mean. How do you create the dates? Can you change it to start from '1/1/1900' instead of '0/1/1900'?

    – perl
    Mar 23 at 9:56












  • @jonboy: I've updated with the line of code that adds one day to your dates in string format before converting to datetimes. Would that work for you?

    – perl
    Mar 23 at 10:10











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55312137%2fhow-to-plot-date-times-over-a-day-matplotlib%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














The problem is that "0/1/1900" is not a correctly formatted date ("1/1/1900" would be fine). We can increment the day of the month by one:



import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as dates

d = (
'Time' : ['0/1/1900 8:10:00', '0/1/1900 12:10:00', '0/1/1900 22:10:00', '1/1/1900 1:10:00'],
'Value' : [1,2,3,4],
)
df = pd.DataFrame(data = d)

df['Time'] = ['/'.join([str(int(x.split('/')[0])+1)] + x.split('/')[1:]) for x in d['Time']]

df['Time'] = pd.to_datetime(df['Time'], format='%d/%m/%Y %H:%M:%S')
df.plot(x='Time', y='Value')
formatter = dates.DateFormatter('%Y-%m-%d %H:%M:%S')
plt.gcf().axes[0].xaxis.set_major_formatter(formatter)


Output:



enter image description here






share|improve this answer

























  • Thanks @perl. But I'm hoping to use the date time to plot values past midnight last. So the value value should be at 8:00:00 and the last one should be at 01:00:00

    – jonboy
    Mar 23 at 9:42











  • OK, I see what you mean. How do you create the dates? Can you change it to start from '1/1/1900' instead of '0/1/1900'?

    – perl
    Mar 23 at 9:56












  • @jonboy: I've updated with the line of code that adds one day to your dates in string format before converting to datetimes. Would that work for you?

    – perl
    Mar 23 at 10:10















1














The problem is that "0/1/1900" is not a correctly formatted date ("1/1/1900" would be fine). We can increment the day of the month by one:



import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as dates

d = (
'Time' : ['0/1/1900 8:10:00', '0/1/1900 12:10:00', '0/1/1900 22:10:00', '1/1/1900 1:10:00'],
'Value' : [1,2,3,4],
)
df = pd.DataFrame(data = d)

df['Time'] = ['/'.join([str(int(x.split('/')[0])+1)] + x.split('/')[1:]) for x in d['Time']]

df['Time'] = pd.to_datetime(df['Time'], format='%d/%m/%Y %H:%M:%S')
df.plot(x='Time', y='Value')
formatter = dates.DateFormatter('%Y-%m-%d %H:%M:%S')
plt.gcf().axes[0].xaxis.set_major_formatter(formatter)


Output:



enter image description here






share|improve this answer

























  • Thanks @perl. But I'm hoping to use the date time to plot values past midnight last. So the value value should be at 8:00:00 and the last one should be at 01:00:00

    – jonboy
    Mar 23 at 9:42











  • OK, I see what you mean. How do you create the dates? Can you change it to start from '1/1/1900' instead of '0/1/1900'?

    – perl
    Mar 23 at 9:56












  • @jonboy: I've updated with the line of code that adds one day to your dates in string format before converting to datetimes. Would that work for you?

    – perl
    Mar 23 at 10:10













1












1








1







The problem is that "0/1/1900" is not a correctly formatted date ("1/1/1900" would be fine). We can increment the day of the month by one:



import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as dates

d = (
'Time' : ['0/1/1900 8:10:00', '0/1/1900 12:10:00', '0/1/1900 22:10:00', '1/1/1900 1:10:00'],
'Value' : [1,2,3,4],
)
df = pd.DataFrame(data = d)

df['Time'] = ['/'.join([str(int(x.split('/')[0])+1)] + x.split('/')[1:]) for x in d['Time']]

df['Time'] = pd.to_datetime(df['Time'], format='%d/%m/%Y %H:%M:%S')
df.plot(x='Time', y='Value')
formatter = dates.DateFormatter('%Y-%m-%d %H:%M:%S')
plt.gcf().axes[0].xaxis.set_major_formatter(formatter)


Output:



enter image description here






share|improve this answer















The problem is that "0/1/1900" is not a correctly formatted date ("1/1/1900" would be fine). We can increment the day of the month by one:



import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as dates

d = (
'Time' : ['0/1/1900 8:10:00', '0/1/1900 12:10:00', '0/1/1900 22:10:00', '1/1/1900 1:10:00'],
'Value' : [1,2,3,4],
)
df = pd.DataFrame(data = d)

df['Time'] = ['/'.join([str(int(x.split('/')[0])+1)] + x.split('/')[1:]) for x in d['Time']]

df['Time'] = pd.to_datetime(df['Time'], format='%d/%m/%Y %H:%M:%S')
df.plot(x='Time', y='Value')
formatter = dates.DateFormatter('%Y-%m-%d %H:%M:%S')
plt.gcf().axes[0].xaxis.set_major_formatter(formatter)


Output:



enter image description here







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 23 at 10:08

























answered Mar 23 at 9:15









perlperl

2,111417




2,111417












  • Thanks @perl. But I'm hoping to use the date time to plot values past midnight last. So the value value should be at 8:00:00 and the last one should be at 01:00:00

    – jonboy
    Mar 23 at 9:42











  • OK, I see what you mean. How do you create the dates? Can you change it to start from '1/1/1900' instead of '0/1/1900'?

    – perl
    Mar 23 at 9:56












  • @jonboy: I've updated with the line of code that adds one day to your dates in string format before converting to datetimes. Would that work for you?

    – perl
    Mar 23 at 10:10

















  • Thanks @perl. But I'm hoping to use the date time to plot values past midnight last. So the value value should be at 8:00:00 and the last one should be at 01:00:00

    – jonboy
    Mar 23 at 9:42











  • OK, I see what you mean. How do you create the dates? Can you change it to start from '1/1/1900' instead of '0/1/1900'?

    – perl
    Mar 23 at 9:56












  • @jonboy: I've updated with the line of code that adds one day to your dates in string format before converting to datetimes. Would that work for you?

    – perl
    Mar 23 at 10:10
















Thanks @perl. But I'm hoping to use the date time to plot values past midnight last. So the value value should be at 8:00:00 and the last one should be at 01:00:00

– jonboy
Mar 23 at 9:42





Thanks @perl. But I'm hoping to use the date time to plot values past midnight last. So the value value should be at 8:00:00 and the last one should be at 01:00:00

– jonboy
Mar 23 at 9:42













OK, I see what you mean. How do you create the dates? Can you change it to start from '1/1/1900' instead of '0/1/1900'?

– perl
Mar 23 at 9:56






OK, I see what you mean. How do you create the dates? Can you change it to start from '1/1/1900' instead of '0/1/1900'?

– perl
Mar 23 at 9:56














@jonboy: I've updated with the line of code that adds one day to your dates in string format before converting to datetimes. Would that work for you?

– perl
Mar 23 at 10:10





@jonboy: I've updated with the line of code that adds one day to your dates in string format before converting to datetimes. Would that work for you?

– perl
Mar 23 at 10:10



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55312137%2fhow-to-plot-date-times-over-a-day-matplotlib%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript